在POST之后已更新并回显到JS的PHP变量保持为空

时间:2019-03-10 11:48:29

标签: javascript php xmlhttprequest

我有一个HTML按钮,一旦用户按下它,它将执行以下POST(到同一issue.php页):

var params = "Bloodtype=" + encodeURIComponent(Bloodtype);
var url = "issue.php?" +params;
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function() 
{
    if(request.readyState == 4 && request.status == 200) 
    {
        checkDoner();
    }
}
request.send(params);

发出请求后,PHP代码将继续检查数据库是否匹配-如果存在匹配,则设置php变量isDoner = 1,否则设置isDoner=0

函数checkDoner()响应PHP变量isDoner并检查其值-通过该值,我需要在html代码中显示/隐藏某个div。

我在发布后检查了所有PHP var值-它们都具有应有的值-问题是当我将它们传递给JS时,它们始终为空。

这是checkDoner()函数:

function checkDoner()
{
    var isDoner = "<?=json_encode($isDoner) ?>";
    if(isDoner == null)
    {
        console.log("In isDoner 1");
        document.getElementById('positiveAnswer').style.display = 'none';
        document.getElementById('negativeAnswer').style.display = 'none';
    }
    else if(isDoner == '0'){
        console.log("In isDoner 2,DONER :" + isDoner);
        document.getElementById('negativeAnswer').style.display = 'block';
    }
    else if(isDoner == '1'){
        console.log("In isDoner 3");
        document.getElementById("pName").innerHTML = "<? echo json_encode($doners['Name']) ?>";
        //$doners array is initiated to null and gets values after post
        document.getElementById("pId").innerHTML =  "<? echo json_encode($doners['Id'])?>";
        document.getElementById("pBloodtype").innerHTML = "<?echo json_encode($doners['Bloodtype'])?>";
         //document.getElementById("pName").innerHTML = <?=json_encode($doners['Name'])?>;
         //document.getElementById("pId").innerHTML = <?=json_encode($doners['Id'])?>;
        //I tried both ways
        document.getElementById('positiveAnswer').style.display = 'block';
    }
}

1 个答案:

答案 0 :(得分:0)

在issue.php页面中,就像chris所说的那样,您可以通过标头检测请求是否为ajax,也可以通过发送get参数'ajaxRequest = 1'来执行此操作。并且在$ _GET中设置了'ajaxRequest'时,您只需要回显json对象并退出。

您的issue.php看起来像这样

if(!empty($_GET["ajaxRequest"])) {
  $data = array();
  ..... your code to get insert values in $data ....
  ..... add $data["isDoner"] = YOUR VALUE .....
  ..... add $data["donors"] = YOUR ARRAY .....
  echo json_encode($data);
  exit();
}

请记住,当您发送ajax请求时,您的整个php代码都在服务器上运行,并且您回显的任何数据或在php块外写入的任何数据( <?php ?> ),将被浏览器接收,您可以通过“ request.responseText”访问该浏览器,其中请求是XMLHttpRequest对象。

您的js应该看起来像这样

var params = "Bloodtype=" + encodeURIComponent(Bloodtype) + "&ajaxRequest=1";
var url = "issue.php?" +params;
var request = new XMLHttpRequest();
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function() 
{
    if(request.readyState == 4 && request.status == 200) 
    {
        checkDoner(request.responseText);
    }
}
request.send(params);

function checkDoner(response)
{
    var isDoner = null, donors=null;
    try {
      response = JSON.parse(response);
      isDoner = response.isDoner;
      donors = response.donors;
    }
    catch(e){  }

    if(isDoner == null)
    {
        console.log("In isDoner 1");
        document.getElementById('positiveAnswer').style.display = 'none';
        document.getElementById('negativeAnswer').style.display = 'none';
    }
    else if(isDoner == '0'){
        console.log("In isDoner 2,DONER :" + isDoner);
        document.getElementById('negativeAnswer').style.display = 'block';
    }
    else if(isDoner == '1'){
        console.log("In isDoner 3");
        document.getElementById("pName").innerHTML = donors.Name;
        //$doners array is initiated to null and gets values after post
        document.getElementById("pId").innerHTML =  donors.Id;
        document.getElementById("pBloodtype").innerHTML = donors.Bloodtype;
         //document.getElementById("pName").innerHTML = donors.Name;
         //document.getElementById("pId").innerHTML = donors.Id;
        //I tried both ways
        document.getElementById('positiveAnswer').style.display = 'block';
    }
}