user1002039 user1002039 - 1 year ago 143
jQuery Question

JQuery - Appending to Serialize

I am trying to figure out how to append two more values to the serialize method in JQuery. I have the following code to submit a form with ajax and have two more variables that I would like to append:

Thank you!

var formData = $('#contact_form').serialize();

// -----------------------------------------------
// -----------------------------------------------
function submitForm(formData){
type: 'POST',
url: 'contact.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
// display success message
error: function(XMLHttpRequest, textStatus, errorThrown) {
complete: function(XMLHttpRequest, status) {

Answer Source

If you change serialize() to serializeArray() you can push values into the array :

var formData = $('#contact_form').serializeArray();
formData.push({ name: "<something>", value: "<somevalue>" });

The data can still be sent in the same way as you would with the serialize() method, using the $.ajax() method