Javascript/PHP 中带有输入参数的 XMLHTTPrequests
XMLHTTPrequests in Javascript/PHP with input parameters
我用这个把头撞在墙上。我的问题的答案应该是 here and here,我不想 post 重复。但出于某种原因,我的代码虽然看起来与这些示例完全相同,但无法正常工作。
我正在尝试通过 xmlhttprequest 将 ID 从 Javascript 函数传递到 PHP 函数。这是我的 Javascript 函数:
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);
}
我的 PHP 文件,acceptRequest.php,看起来像这样:
<?php
echo $_POST['id_Request'];
?>
发生的事情是 Javascript 函数确实发送了警报:"Request accepted." 但是,PHP 函数没有回显任何内容。我在这里错过了什么???
我想问题是你的函数没有传递任何 id。我确定在您的 php/javascript 中。您要传递的ID在某个输入框中。
然后在你的js函数中,你可以说
Var id = document.getElementById("id-of_input").value;
然后您可以将其附加到您的 Ajax 请求中。
我还注意到 Ajax 没有收到 php 的回复。
if(xmlhttp.readyState == 4 &&. xmlhttp.status == 200){
document.getElementById(where).innerHTML = xmlhttp.responseText;
}
在你的情况下,它会是 xhttp.responseText;
该行仅表示您希望显示 Ajax 请求结果的位置。
我用这个把头撞在墙上。我的问题的答案应该是 here and here,我不想 post 重复。但出于某种原因,我的代码虽然看起来与这些示例完全相同,但无法正常工作。
我正在尝试通过 xmlhttprequest 将 ID 从 Javascript 函数传递到 PHP 函数。这是我的 Javascript 函数:
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);
}
我的 PHP 文件,acceptRequest.php,看起来像这样:
<?php
echo $_POST['id_Request'];
?>
发生的事情是 Javascript 函数确实发送了警报:"Request accepted." 但是,PHP 函数没有回显任何内容。我在这里错过了什么???
我想问题是你的函数没有传递任何 id。我确定在您的 php/javascript 中。您要传递的ID在某个输入框中。
然后在你的js函数中,你可以说
Var id = document.getElementById("id-of_input").value;
然后您可以将其附加到您的 Ajax 请求中。
我还注意到 Ajax 没有收到 php 的回复。
if(xmlhttp.readyState == 4 &&. xmlhttp.status == 200){
document.getElementById(where).innerHTML = xmlhttp.responseText;
}
在你的情况下,它会是 xhttp.responseText;
该行仅表示您希望显示 Ajax 请求结果的位置。