I'm playing around a little with Ajax and i'm using the following code.
var r = new XMLHttpRequest();
r.open("POST", "pythonTesting.php", true);
r.onreadystatechange = function () {
if (r.readyState === 4 && r.status === 200) {
alert("Success: " + r.responseText);
}
};
r.send("text=yellow");
It is successful but the alert isn't showing the actual responseText
.
pythonTesting.php (not to be confused by the name) is just:
<?php
echo $_POST['text'];
?>
I'm expecting the responseText
to be yellow
because that's how I sent it but its not. I'm not really seeing what the problem is though. It must be one of those easy to fix mistakes.
from http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp:
xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
Since this is a post I believe you will need to set the length:
r.setRequestHeader("Content-length", "text=yellow".length);
send the variables through the address:
r.open("POST", "pythonTesting.php?text=yellow", true);
then retrieve the variable in php by
$text = $_REQUEST['text'];