why adds whitespace to data sent with ajax?

I

Igal

i just started working with ajax, and werid thing happens.
i have a function that build my post string for ajax to send. it looks
like this:

function buildPOST(formName, formTask) {
theForm = document.getElementById(formName);
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='' && theForm.elements[e].type !
= 'button' && theForm.elements[e].type != 'submit') {
var name = theForm.elements[e].name;
qs+=(qs=='')?'':'&'
qs+= name+'='+escape(theForm.elements[e].value);
}
}
if (formTask != null) { qs+="&task="+formTask; }
qs+="\n";
return qs
}

and this how i send it...
var postString = buildPOST(...);
....
xmlHttp.send(postString);

i have noticed that when i us $_POST['task'] with PHP (the last var in
the line) its actually that data + whitespace, so i need to do
trim($_POST['task']), question is why this happens? i don't think it's
the buildPOST func, since i tried tirming befor sending with ajax.
 
V

VK

function buildPOST(formName, formTask) {
theForm = document.getElementById(formName);
var qs = ''
for (e=0;e<theForm.elements.length;e++) {
if (theForm.elements[e].name!='' && theForm.elements[e].type !
= 'button' && theForm.elements[e].type != 'submit') {
var name = theForm.elements[e].name;
qs+=(qs=='')?'':'&'
qs+= name+'='+escape(theForm.elements[e].value);
}
}
if (formTask != null) { qs+="&task="+formTask; }
qs+="\n";
return qs

}

and this how i send it...
var postString = buildPOST(...);
...
xmlHttp.send(postString);

For crying out loud... No, I don't know why extra space, just use the
many years tested http://www.ajaxtoolbox.com for manual POST
submittal. See http://www.ajaxtoolbox.com/request/examples.php for
"Form Submittal" demo. As a small payback could you tell me why are
doing manually something what any single browser on the market does
automatically and reliably? I am just getting really curious.
 
T

Thomas 'PointedEars' Lahn

Igal said:
function buildPOST(formName, formTask) {
[...]
qs+="\n";
return qs
}

and this how i send it...
var postString = buildPOST(...);
...
xmlHttp.send(postString);

i have noticed that when i us $_POST['task'] with PHP (the last var in
the line) its actually that data + whitespace, so i need to do
trim($_POST['task']), question is why this happens? i don't think it's
the buildPOST func, since i tried tirming befor sending with ajax.

Since you have not posted how you try to trim the data to be submitted
before the request, it is likely that the additional unnecessary "\n" causes
this in PHP.

Please use a spell-checker, learn enough English to write something easily
understandable yourself, or try a local newsgroup next time instead.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top