Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Posting Form Data Through AJAX

Alternative to building a string? If not, getting all values of multi field



3:15 am on Feb 3, 2010 (gmt 0)

5+ Year Member

First off, I am trying to post form data through an ajax call to send to a php script. The only way I can find is by assigning the current form fields and values to vars and then making a string to pass.

If there is a way to directly post data like a normal form I would much rather that. I have found a way that almost works.

I am wanting to get all of the values in a form to be passed through without having to predefine each of them. I use this script (not exactly this):

for(i=0; i<document.form_name.elements.length; i++)
field_string += " & " + document.form_name.elements[i].name + " = " + document.form_name.elements[i].value;

The problem is that it doesn't get all of the values for a multiple field list. It only grabs the first one selected. Is there a way to check to see if a field is an array in JS and get the values?


6:05 am on Feb 3, 2010 (gmt 0)

5+ Year Member

I can't seem to get the "post" to pass through although I can send vars with get...

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

Parameter looks like this:



1:45 pm on Feb 3, 2010 (gmt 0)

10+ Year Member

Try this:

var parameters= 'field1=' + document.getElementById('field1').value;
parameters+= '&field2' + document.getElementById('field2').value;

HTH, Tom


3:04 pm on Feb 3, 2010 (gmt 0)

5+ Year Member

When I get parameters, it returns the string I have above. My problem lies in [assing the string as a post through the function...I'm not sure if it is the header setup or what I am doing wrong


4:17 pm on Feb 3, 2010 (gmt 0)

5+ Year Member

Make sure that parameters has a value! This now works for me after much turmoil..

xmlhttp.open("POST", "includes/prop_upload_form.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "close");

Featured Threads

Hot Threads This Week

Hot Threads This Month