1

I'm having a lot of trouble trying to send a variable through the parameters through post data via an ajax request, I cannot get it to work for some odd reason. When I use what the actual variable contains, there is no problem, but I cannot do that since it changes every time, so instead of using something hard-coded I wanted to try using a variable, the charset of the characters it contains is always the same though.

var http=new XMLHttpRequest();
uri='http://www.example.com/profile.php';
http.open('POST',uri,true);
params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+';
http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
http.setRequestHeader('Content-length',params.length);
http.setRequestHeader('Connection', 'close');
http.onreadystatechange=function(){
if(http.readyState==4&&http.status==200){
//alert(http.responseText);
}
};
http.send(params);

What is the correct syntax to include a variable alongside with some plain-text? Or simply how can I make this work?

0

2 Answers 2

2

You have a stray quote:

Change:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+';

to:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable;

Also, make sure to escape your variables properly:

params='name=Tom&lastname=Jordan&'+variable+'='+encodeURIComponent(secondvariable);
Sign up to request clarification or add additional context in comments.

1 Comment

Ok i'll give it a try right now and tell you if i have any problems.
0

You have an extra single-quote:

params='name=Tom&lastname=Jordan&'+variable+'='+secondvariable+';

The +' at the end isn't necessary, and it's treating the semi-colon as a string. (Probably resulting in a syntax error.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.