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
  • 98,240
  • 88
  • 296
  • 433
Johan
  • 1,897
  • 5
  • 23
  • 29

3 Answers3

94

Use serialize method.

data :   $("form").serialize()
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
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,190
  • 4
  • 27
  • 32
2

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,803
  • 20
  • 11