35

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!

abatishchev
  • 95,331
  • 80
  • 293
  • 426
Johan
  • 1,857
  • 5
  • 23
  • 28

3 Answers3

94

Use serialize method.

data :   $("form").serialize()
SolutionYogi
  • 30,954
  • 12
  • 69
  • 77
10

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

That would make the following example:

$("#submit").click(function() {
    $.ajax({
        data: $("form").serialize(),
        ...rest
    });
});
Dykam
  • 10,112
  • 4
  • 26
  • 32
1

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

JoyGuru
  • 1,675
  • 19
  • 9