I have an HTML button that once a user presses it , it preforms the following POST (to the same issue.php
page):
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);
Once the request has been made, PHP code goes on checking the database for a match - if there is a match I set a php variable isDoner = 1
, else isDoner=0
.
The function checkDoner()
echos the PHP var isDoner
and checks for its value - and by that value I need to show/hide a certain div in the html code.
I have checked all PHP var values after the post - they all have the values that they should have - the problem is when I pass them to JS they are always null.
This is the checkDoner()
function :
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';
}
}