Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax - submit all elements in form without manually entering them [duplicate]

I would like to use jQuery.ajax to submit a form using POST without having to specify everything manually in the "data: " part.

This is what I don't want:

data:   "username=" + document.getElementById("username").value + 
    "&email=" + document.getElementById("email").value,

Is there a way to just have it include alla elements with their values from an entire FORM field? This form is generated dynamically so it would save me a lot of time!

like image 787
Johan Avatar asked Aug 04 '09 18:08

Johan


3 Answers

Use serialize method.

data :   $("form").serialize()
like image 122
SolutionYogi Avatar answered Sep 23 '22 06:09

SolutionYogi


Look at http://docs.jquery.com/Ajax/serialize.

That would make the following example:

$("#submit").click(function() {
    $.ajax({
        data: $("form").serialize(),
        ...rest
    });
});
like image 22
Dykam Avatar answered Sep 23 '22 06:09

Dykam


Use .serialize() method to send entire form data in jQuery Ajax.

data:$('#formID').serialize()

Example script can be found from here - How to send entire form data in jQuery Ajax

like image 1
JoyGuru Avatar answered Sep 21 '22 06:09

JoyGuru