I am banging my head against the wall with this one. The answer to my question should be here and here, and I don't want to post a duplicate. But for some reason, my code, though seemingly exactly the same as these examples, won't work.
I'm trying to pass an ID from a Javascript function to a PHP function via an xmlhttprequest. Here is my Javascript function:
function acceptRequest(id_Request)
{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "acceptRequest.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
alert("Request accepted.");
};
};
xhttp.send("id_Request="+id_Request);
}
My PHP file, acceptRequest.php, looks like this:
<?php
echo $_POST['id_Request'];
?>
What happens is that the Javascript function does send the alert: "Request accepted." But, the PHP function doesn't echo anything out. What am I missing here???
I guess the problem is that your function isn't passing any id. I'm sure in your php/javascript. The id you want to pass is in some input box.
Then in your js function, you can say
Var id = document.getElementById("id-of_input").value;
You can then append this to you Ajax request.
I also noticed that your Ajax get no response from the php.
if(xmlhttp.readyState == 4 &&. xmlhttp.status == 200){
document.getElementById(where).innerHTML = xmlhttp.responseText;
}
In your case, it'ld be xhttp.responseText;
That line just means where you wish to display the results of the Ajax request.