简体   繁体   中英

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. Here is my html file

<!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
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"

Seems like there are few issues

  1. On the setRequestHeader (which takes 2 arguement, but were only given one in your sample code)

    hr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

  2. Your php code needs to return a json format eg

    echo '{"message":"'.$_POST['test'].'"}';

  3. Your page needs to get the value from the parsed json (note the 'message' as per the php code)

    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.

Check your browser's developer console to check the output of your PHP script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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