Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize multiple forms together?

Can you serialize multiple forms into one so that only one post or ajax request is made? I have searched around and it is all for submiting each form separently via post/ajax.

like image 985
arbme Avatar asked Feb 14 '12 16:02

arbme


People also ask

How to serialize two form in jQuery?

If you run $('form'). serialize() on a page with multiple forms, it will correctly serialize all the forms into one string. Show activity on this post. When you use the jQuery serialize() function, it simply turns your form into a string in the format a=1&b=2&c=3 .

How do you serialize a form?

jQuery serialize() MethodThe serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.


2 Answers

If you run $('form').serialize() on a page with multiple forms, it will correctly serialize all the forms into one string.

To include only certain forms, use $('#form1, #form2').serialize()

like image 96
472084 Avatar answered Oct 16 '22 13:10

472084


When you use the jQuery serialize() function, it simply turns your form into a string in the format a=1&b=2&c=3. So you can certainly apply this function to two forms and concatenate the result, with an & between them, and use the result in your ajax call. You'd want some checks to make sure neither string is empty when you do the concatenation.

like image 38
Jacob Mattison Avatar answered Oct 16 '22 13:10

Jacob Mattison