/    Sign up×
Community /Pin to ProfileBookmark

Sending multiple post variables via ajax problems

Hi there, I’m trying to send multiple post vars to a php script but having a good bit of trouble. Here’s the code:

[CODE]var titleText = document.getElementById(“notesTitle”).value; //get the title
var notesText = document.getElementById(“notesText”).innerHTML; // get the content
titleText = encodeURIComponent(titleText); //uri escape title
notesText = encodeURIComponent(notesText); //uri escape text
titleText = “title=”+titleText; //set them up as post variables
notesText = “notes=”+notesText;
var postArray = new Array(2); //put them in an array that I will hand to the postAjax function
postArray[0] = titleText;
postArray[1] = notesText;
var responseText = postAjax(‘/newSchool/php/notesSubmitter.php’,postArray,’writingPlace’);

function postAjax(file,postvar,div)
{
var httpObject = getHttpObject();
httpObject.open(‘POST’,file,false);
for(var i=0;i<postvar.length;i++)
{
httpObject.send(postvar[i]);
}
if(httpObject.status == 200)
{
var str = httpObject.responseText;
}
else
{
document.write(“404 Not Found. There may have been an error in the request”);
}
return str;
}
[/CODE]

When I run it I get an error entitled “INVALID_STATE_ERR: DOM Exception 11”. Does anyone know what I’m doing wrong (I understand I might have done all this completely wrong, sorry if I did)?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMar 25.2008 — You don't send the POST variables one at a time. You send them all a once. Try changing:
for(var i=0;i&lt;postvar.length;i++)
{
httpObject.send(postvar[i]);
}

to
httpObject.send( postvar.join("&amp;") );
The postvar variable is an array and needs to be a series of foo=bar variable-value pairs. The [B].join()[/B] method of the Array object takes each item in the array and joins it into a string separated by the string you pass to the .join() function.
Copy linkTweet thisAlerts:
@jake_dauthorMar 25.2008 — Thanks a lot, I eventually figured out I needed to just send all the post vars in one string, but that join idea is a good one. Also, I think it might be mandatory to set the headers, although this might actually be optional. Here's the now working code.

[CODE]function postAjax(file,postvar,div)
{
var array = postvar;
var str = 'vars not set';
var formattedPostVariables = '';
for(var i=0;i<array.length;i++)
{
formattedPostVariables = formattedPostVariables + array[i];
if(i+1 < array.length)
{
formattedPostVariables = formattedPostVariables + '&';
}
}
alert(formattedPostVariables);
var httpObject = getHttpObject();
httpObject.open('POST',file,false);
httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpObject.setRequestHeader("Content-length", formattedPostVariables.length);
httpObject.setRequestHeader("Connection","close");
httpObject.send(formattedPostVariables);

if(httpObject.status == 200)
{
str = httpObject.responseText;
}
else
{
document.write("404 Not Found. There may have been an error in the request");
}
return str;
}[/CODE]
Copy linkTweet thisAlerts:
@toicontienMar 25.2008 — I believe you do need to set the content-type header, otherwise I think the XMLHttpRequest object sends application/xml.
×

Success!

Help @jake_d spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...