homepage Welcome to WebmasterWorld Guest from 54.161.192.130
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Posting Form Data Through AJAX
Alternative to building a string? If not, getting all values of multi field
wdavis10

5+ Year Member



 
Msg#: 4073229 posted 3:15 am on Feb 3, 2010 (gmt 0)

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?

 

wdavis10

5+ Year Member



 
Msg#: 4073229 posted 6:05 am on Feb 3, 2010 (gmt 0)

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


xmlhttp.open("POST","script.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameters);


Parameter looks like this:

thing=value&thing_2=value_2&thing_3=value_3

Arno_Adams

5+ Year Member



 
Msg#: 4073229 posted 1:45 pm on Feb 3, 2010 (gmt 0)

Try this:

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

HTH, Tom

wdavis10

5+ Year Member



 
Msg#: 4073229 posted 3:04 pm on Feb 3, 2010 (gmt 0)

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

wdavis10

5+ Year Member



 
Msg#: 4073229 posted 4:17 pm on Feb 3, 2010 (gmt 0)

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");
xmlhttp.send(parameters);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved