Reputation: 79
I'm sending data via Jquery Ajax post, I want to send the text box from my form, but also a variable called username. For example...
$.ajax({type:'POST', url: 'submit.php', data:$('#myform').serialize(), username});
How can I do this?
Upvotes: 0
Views: 272
Reputation: 794
Try this
$.ajax({
type:'POST',
url: 'submit.php',
data: { form: $('#myform').serialize(), username: encodeURIComponent(username)}
});
check ajax documentation the part Sending Data to the Server
Upvotes: 0
Reputation: 97682
Just add the extra parameter to the serialized string
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize()+'&username='+encodeURIComponent(username)
});
Upvotes: 1
Reputation: 298
As a good practice, it is better to construct a data object and also include the extra variables. Try constructing a data object and include them ?
data = {
'formData': $('#myform').serialize(),
'variable': 'username'
}
Upvotes: 1
Reputation: 19573
You can pass it your other variable via get like this.
$.ajax({type:'POST', url: 'submit.php?myvar='+encodeURIComponent(myvar), data:$('#myform').serialize(), username});
You could also add the value to a hidden field in your form.
Upvotes: 0
Reputation: 5239
Try,
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username: username //you missed the key
});
Upvotes: 0
Reputation: 45124
try the query below. username should be a js variable.
var username = "myname";
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username:username
});
Upvotes: 0