drex88669 2016-05-13 05:45
浏览 39
已采纳

带有输入参数的Javascript / PHP中的XMLHTTP请求

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???

  • 写回答

1条回答 默认 最新

  • doujingjiao0015 2016-05-13 05:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应