lollo
lollo

Reputation: 165

ajax request hidden fields

I'm developing a site (only for fun and learn programming with jquery) and i'd like to know what's wrong with this :

$(window).unload(function(){
 var myid = $('input#v1').attr('value');   // hidden
 var playauth = $('input#v2').attr('value');   // hidden
 var srvid = $('input#v3').attr('value');      // hidden
 var result = 'myid='+ myid +'&auth='+ playauth +'&srvid='+ srvid;
    $.ajax({
            type: "GET",
            data: result,
            url: "closing.php",
            complete: function(data) {
            alert(data.responseText);
            }
           });
});

I'm trying to update a database table. When i close the window nothing happens. With a previous version of this function :

window.onunload = function () {
  var xhReq = new XMLHttpRequest();
  var n = document.getElementById("v1").InnerHTML;
  var o = document.getElementById("v2").InnerHTML;
  var p = document.getElementById("v3").InnerHTML;
  xhReq.open("GET", ("closing.php?myid=" + n + "&auth=" + o + "&srvid=" + p) , false);
  xhReq.send(null);
  var serverResponse = xhReq.responseText;
  alert(serverResponse); 
};

.. i saw the response alert but GET values were 'undefined'. .... probably because the type of inputs is hidden..? This is my form... maybe i miss something ?? I'm really new to jquery/ajax .. please help!!

<form method="get">
<input id="v1" type="hidden" name="val1" class="aget" value="<?php echo $_GET['myid']; ?>" />
<input id="v2" type="hidden" name="val2" class="bget" value="<?php echo $_GET['playauth']; ?>" />
<input id="v3" type="hidden" name="val3" class="cget" value="<?php echo $_SESSION['srvid']; ?>" />
</form> 

Upvotes: 0

Views: 1099

Answers (2)

Nicol&#225;s Torres
Nicol&#225;s Torres

Reputation: 1345

You must use

.val(); instead of .attr('value');

Upvotes: 2

Kishore
Kishore

Reputation: 1912

change

 var myid = $('input#v1').attr('value');   // hidden
 var playauth = $('input#v2').attr('value');   // hidden
 var srvid = $('input#v3').attr('value');      // hidden

to

 var myid = $('input#v1').val();   // hidden
 var playauth = $('input#v2').val();   // hidden
 var srvid = $('input#v3').val();      // hidden

Upvotes: 2

Related Questions