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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度