JQuery form: serialize () and other parameters

Is it possible to submit form elements (serialized with .serialize() method) and other parameters with a single AJAX request?

Example:

 $.ajax({ type : 'POST', url : 'url', data : { $('#form').serialize(), par1 : 1, par2 : '2', par3: 232 } } 

If not, what is the best way to submit the form along with other parameters?

thank

+102
jquery ajax serialization forms
May 01 '12 at 14:01
source share
7 answers

serialize() effectively turns form values ​​into a valid sequence of requests, so you can simply add to the line:

 $.ajax({ type : 'POST', url : 'url', data : $('#form').serialize() + "&par1=1&par2=2&par3=232" } 
+213
May 01 '12 at 2:04
source share

Alternatively, you can use form.serialize() with $.param(object) if you store your parameters in some object variable. Usage will be:

 var data = form.serialize() + '&' + $.param(object) 

See http://api.jquery.com/jQuery.param for future reference.

+69
Nov 22
source share

I don’t know, but none of the above worked for me. Then I used this and it worked:

As a serialized array, it is stored as a pair of key values

We clicked a new value or values ​​here in a form variable, and now we can pass this variable right now.

 var form = $('form.sigPad').serializeArray(); var uniquekey = { name: "uniquekey", value: $('#UniqueKey').val() }; form.push(uniquekey); 
+8
Jul 15 '15 at 8:54
source share

If you want to submit form serialized data, you can try this

 var form= $("#formId"); $.ajax({ type: form.attr('method'), url: form.attr('action'), data: form.serialize()+"&variable="+otherData, success: function (data) { var result=data; $('#result').attr("value",result); } }); 
+1
Sep 11 '18 at 8:17
source share

You can create a helper form using jQuery with the contents of another form, and then add other parameters to this form, so you only need to serialize it in an ajax call.

 function createInput(name,value){ return $('<input>').attr({ name: name, value: value }); } $form = $("<form></form>"); $form.append($("#your_form input").clone()); $form.append(createInput('input_name', 'input_value')); $form.append(createInput('input_name_2', 'input_value_2')); .... $.ajax({ type : 'POST', url : 'url', data : $form.serialize() } 
0
Oct 26 '16 at 11:10
source share

I fix the problem with the expression; send data using the same get method

 $.ajax({ url: 'includes/get_ajax_function.php?value=jack&id='+id, type: 'post', data: $('#b-info1').serializeArray(), 

and get the value using $_REQUEST['value'] OR $_GET['id']

-one
Sep 17 '17 at 8:16 on
source share

You can also use the serializeArray function to do the same.

-one
Jul 17 '19 at 6:01
source share



All Articles