Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  146] [ 2]  / answers: 1 / hits: 145377  / 13 Years ago, wed, november 9, 2011, 12:00:00

How do I send variables to the server with XMLHTTPRequest? Would I just add them to the end of the URL of the GET request, like ?variable1=?variable2=, etc?



So more or less:



XMLHttpRequest(GET, blahblah.psp?variable1=? + var1 + ?variable2= + var2, true)

More From » html

 Answers
1

If you want to pass variables to the server using GET that would be the way yes. Remember to escape (urlencode) them properly!



It is also possible to use POST, if you dont want your variables to be visible.



A complete sample would be:



var url = bla.php;
var params = somevariable=somevalue&anothervariable=anothervalue;
var http = new XMLHttpRequest();

http.open(GET, url+?+params, true);
http.onreadystatechange = function()
{
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);


To test this, (using PHP) you could var_dump $_GET to see what you retrieve.


[#89226] Monday, November 7, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alonso

Total Points: 747
Total Questions: 108
Total Answers: 105

Location: Mauritania
Member since Sun, Sep 25, 2022
2 Years ago
;