简体   繁体   English

post变量没有传递给php

[英]post variable not passed to php

I am trying to send a POST variable through an AJAX call and it seems the php script isn't receiving it. 我试图通过AJAX调用发送一个POST变量,似乎PHP脚本没有收到它。 Here is my html file 这是我的html文件

<!doctype html>
<html>
<head>
    <title>Testing</title>
</head>
<body>
    <h1 id="test">Testing</h1>
    <input type="text" id="search" placeholder="search"/>
    <div id="target">
    </div>
<script type="text/javascript">
function ajax_call(){
    var hr=new XMLHttpRequest();
    var target=document.getElementById("target");
    hr.open("POST", "phptest.php", true);
    hr.setRequestHeader("Content-Type"," application/x-www-form-urlencoded");
    hr.onreadystatechange=function(){
        if(hr.readyState==4 && hr.status==200){
            var data=(hr.responseText);
            target.innerHTML="";
            target.innerHTML+=data;

        }
    }
    hr.send("test="+document.getElementById("test").innerHTML);
}
ajax_call();
</script>
<!-- <script type="text/javascript" src="Scripts/friends2.js"></script> -->
</body>
</html>

And here is the PHP file :- 这是PHP文件: -

<?php
header("Content-Type: application/json");
//$search=$_POST['search'];
/*if(empty($_POST)){
    echo "not set";
}*/

echo $_POST['test'];
?>

I removed the JSON parse and when i uncomment that if(empty..) line i see "not set" 我删除了JSON解析,当我取消注释if(empty ..)行时,我看到“未设置”

Seems like there are few issues 好像很少有问题

  1. On the setRequestHeader (which takes 2 arguement, but were only given one in your sample code) 在setRequestHeader上(需要2个争论,但在示例代码中只给出了一个)

    hr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); hr.setRequestHeader( “内容类型”, “应用程序/ x WWW的形式进行了urlencoded”);

  2. Your php code needs to return a json format eg 你的PHP代码需要返回一个json格式,例如

    echo '{"message":"'.$_POST['test'].'"}'; echo'{“message”:“'。$ $ POST ['test']。'”}};

  3. Your page needs to get the value from the parsed json (note the 'message' as per the php code) 您的页面需要从解析的json中获取值(请根据php代码注意'message')

    target.innerHTML+=data.message; target.innerHTML + = data.message;

You need to modify content-type to the correct format 您需要将内容类型修改为正确的格式

<!doctype html>
<html>
<head>
    <title>Testing</title>
</head>
<body>
    <h1 id="test">Testing</h1>
    <input type="text" id="search" placeholder="search"/>
    <div id="target">
    </div>
<script type="text/javascript">
function ajax_call(){
    var params = "test="+document.getElementById("test").innerHTML;
    console.log(params);
    var hr=new XMLHttpRequest();
    var target=document.getElementById("target");
    hr.open("POST", "phptest.php", true);
    hr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    hr.onreadystatechange=function(){
        if(hr.readyState==4 && hr.status==200){
            var data=hr.responseText;
            target.innerHTML="";
            target.innerHTML+=data;

        }
    }
    hr.send(params);
}
ajax_call();
</script>
<!-- <script type="text/javascript" src="Scripts/friends2.js"></script> -->
</body>
</html>

You may want to remove the JSON.parse() so you can see any debugging you may be doing on your PHP side, otherwise you need to change your PHP to bring back a JSON string. 您可能希望删除JSON.parse(),以便可以在PHP端看到您可能正在进行的任何调试,否则您需要更改PHP以恢复JSON字符串。

Check your browser's developer console to check the output of your PHP script. 检查浏览器的开发人员控制台以检查PHP脚本的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM