Dxtr Dxtr - 5 months ago 10
jQuery Question

submit Form and String using Ajax

How to submit both a Form and a String using Ajax

I tried this but didn't work, As I can't get

$_POST['foo'])
in PHP page :


$(document).ready(function(){
$('#togreenform').submit(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'operations.php?r=togreen',
data: { $(this).serialize(),
'foo': 'bar'},
success: function(msg){
alert(JSON.parse(msg));
},
error: function(){
//alert('failure');
}
})
return false; // prevent form from submitting
});
});

Answer

Your data property should look like this :

data : {
    foo: 'bar',
    otherprop: 'foo'
};

or

data : $(this).serialize()+"&foo=bar"

The serialize function create a string creates a text string in standard URL-encoded notation.

Your code is producing an invalid object since you don't provide a value with your first key (which is the text string).

Comments