Active3 months ago
In this case, jQuery serializes the successful controls within the form. Only form elements are examined for inputs they contain, in all other cases the input elements to be serialized should be part of the set passed to the.serialize method. Selecting both the form and its children in a set will cause duplicates in the serialized string. I have a jQuery ajax function and would like to submit an entire form as post data. We are constantly updating the form so it becomes tedious to constantly update the form inputs that should be sent in the request.
I have a jQuery ajax function and would like to submit an entire form as post data. We are constantly updating the form so it becomes tedious to constantly update the form inputs that should be sent in the request.
BrianBrian10.5k4949 gold badges119119 silver badges161161 bronze badges
7 Answers
There's a function that does exactly this:
Will VousdenWill Vousden26.3k99 gold badges6767 silver badges8888 bronze badges
serialize() is not a good idea if you want to send a form with post method. For example if you want to pass a file via ajax its not gonna work.
Suppose that we have a form with this id : 'myform'.
the better solution is to make a FormData and send it:
Moh ArjmandiMoh Arjmandi
In general use
serialize()
on the form element.Please be mindful that multiple <select> options are serialized under the same key, e.g.
will result in a query string that includes multiple occurences of the same query parameter:
which may not be what you want in the backend.
I use this JS code to reduce multiple parameters to a comma-separated single key (shamelessly copied from a commenter's response in a thread over at John Resig's place):
which turns the above into:
In your JS code you'd call it like this:
Hope that helps.
nikolanikola![Jquery ajax form serialize php Jquery ajax form serialize php](https://image.slidesharecdn.com/jquery-ajax-2-151217125649/95/javascript-and-jquery-programming-tutorial-jquery-ajax-support-sending-data-to-the-server-6-638.jpg?cb=1450465853)
1,50422 gold badges2222 silver badges4242 bronze badges
Use
Serialize a form to a query string, that could be sent to a server in an Ajax request.
rahulrahul157k4444 gold badges208208 silver badges252252 bronze badges
A good jQuery option to do this is through FormData. This method is also suited when sending files through a form!
Your send function in jQuery would look like this:
to add data to your form you can either use a hidden input in your form, or you add it on the fly:
patrickpatrick8,81855 gold badges5454 silver badges6969 bronze badges
You just have to post the data. and Using jquery ajax function set parameters. Here is an example.
DANISH TAYYIBDANISH TAYYIB
The other solutions didn't work for me. Maybe the old DOCTYPE in the project I am working on prevents HTML5 options.
My solution:
js:
rubo77rubo779,0961515 gold badges8080 silver badges165165 bronze badges
Not the answer you're looking for? Browse other questions tagged jqueryajax or ask your own question.
Active3 months ago
I want to add extra data after i use $('#myForm').serialize() + extra data
Joko WandiroJoko Wandiro1,34211 gold badge1313 silver badges2727 bronze badges
3 Answers
What kind of data?
![Jquery Ajax Serialize Form Jquery Ajax Serialize Form](/uploads/1/2/6/0/126099011/921052501.png)
The 'data' parameter is just a URL encoded string. You can append to it however you like. See the API here.
jthompsonjthompson5,72622 gold badges2828 silver badges3030 bronze badges
Personally, I'd append the element to the form instead of hacking the serialized data, e.g.
That way, you don't have to worry about
Bastien Jansen?
or &
7,63622 gold badges2727 silver badges4949 bronze badges
steadwebsteadwebJquery Ajax Form Serialize Post
6,43611 gold badge1515 silver badges2727 bronze badges
PrakashGPrakashGJquery Ajax Example
1,56744 gold badges1414 silver badges2525 bronze badges