I have a form with a pile of input fields. I want to make a ajax GET request with all of the fields! Easiest way so far looks like assigning the inputs to a data object:
$('#myForm').find('input').each(function(index){
myData = $.data($('#myForm'), $(this).attr('name'), $j(this).val());
});
...and then pump it through the ajax:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = myData,
error(function(){}),
success(function(){});
});
But of course it doesn't work... no $_GET variables show up in the otherpage.php, and the console shows that myData
is some huge object deal.
How do you send data through ajax like this? Is there a better way?
I have a form with a pile of input fields. I want to make a ajax GET request with all of the fields! Easiest way so far looks like assigning the inputs to a data object:
$('#myForm').find('input').each(function(index){
myData = $.data($('#myForm'), $(this).attr('name'), $j(this).val());
});
...and then pump it through the ajax:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = myData,
error(function(){}),
success(function(){});
});
But of course it doesn't work... no $_GET variables show up in the otherpage.php, and the console shows that myData
is some huge object deal.
How do you send data through ajax like this? Is there a better way?
Use the jQuery serialize();
method:
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
http://api.jquery./serialize/
$.ajax({
type:"GET",
url: '/otherpage.php',
data = $('#myForm').serialize(),
error(function(){}),
success(function(){});
});
Hope it'will help you.