Jquery.load () for POST of all form elements, not for indicating what for POST

I have an existing HTML form that I am trying to update to use jQuery.load (). How to pass all form elements in the form of POST parameters, and not indicate which parameters should pass?

Form elements are created dynamically using a script, and the number of elements, as well as the name of the elements, varies significantly to the extent that it is impractical to specify which parameters should pass through AJAX / POST. Is there an easy way to pass jQuery.load () all the elements in the <form></form> tags, as if the form was submitted traditionally?

+7
source share
2 answers

You can use .serialize() to serialize all form inputs to submit along with a jQuery.load () call.

 $('form').serialize() 

For example, using jQuery.load() (only GET, if you do not pass it an object for data, then POST)

 $.load( 'postTo.php', $('#yourFormId').serialize(), complete(responseText, textStatus, XMLHttpRequest){ //do your processing after the fact })) 

Using jQuery.ajax() you can do a POST

 $.ajax({ 'url': 'postTo.php', 'type': 'POST', 'data': $('#yourFormId').serialize(), 'success': function(result){ //process here } }); 

See: http://api.jquery.com/jQuery.ajax/

+18
source

Easy . When you collect form data and pass it as the second parameter in load() , use serializeArray(data) and do not use serialize(data) , as the currently accepted answer recommends.

serialize() returns a string, while serializeArray() returns an object. load() sends a POST if the data is an object. If the data is a string, load() sends a GET.

+25
source

All Articles