SaltyNuts
SaltyNuts

Reputation: 5168

Capturing entire form input data with JavaScript and sending it over AJAX

I have a dynamically generated form that I would like to submit using Ajax. The form contains some input fields and some checkboxes - so the total number of checkboxes that will be submitted is obviously unknown at the start. By "dynamically generated" I mean the contents and the id's of the form elements are pulled from a DB and again, I never know how many there will be in advance.

From what I understand, to submit it with Ajax I need to execute something like this in my javascript function:

xmlhttp.open("GET","/incl/myfile.php?var[]=a&var[]=b&...", true);

My problem is generating those "var[]=a&var[]=b&..." lists. Particularly, I have a hard time understanding how to generate it for those checkboxes, which may or may not be checked.

Is there something that will capture the entire content of the form input fields and format it into a nice GET url query? A plugin perhaps? Something in JQuery maybe?

Upvotes: 0

Views: 619

Answers (2)

Marc B
Marc B

Reputation: 360772

You want jQuery's .serialize() method.

Upvotes: 1

mpj
mpj

Reputation: 5367

Yes, take a look at jQuery's serialize method. http://api.jquery.com/serialize/

Upvotes: 1

Related Questions