I have a PHP function which returns true or false as a String (this works). If I look into my JS file I make an alert box with the responseText of the XMLHttprequest (this works as well). But as soon as I try to compare the responsestring to another string the outcome is always false.
I've already searched stackoverflow but did not find an answer. I also tried str.equals(""), as well as returning a boolean with php but nothing seemed to work.
Javascript:
xhttp.onload = function(){
alert(xhttp.responseText); //Here comes "true" or "false" just like I want it
if(xhttp.responseText == "true"){ //This always gives me "ERROR and then the responseText (which is true or false)
alert("TRUE");
}else if(xhttp.responseText == "false"){
alert("FALSE");
}else{
alert("ERROR" + xhttp.responseText);
}
};
PHP:
if(count($echo)==3){
if($tag==$echo[2]){
echo "true";
break;
}else if($i == (count($fahrten)-1)){
echo "false";
}
}
Expected result: alert with "TRUE" or "FALSE" actual result: alert with "Error" followed by the responseText
I hope some of you can help me and the question is not too stupid I'm stil learning an it is for a scholl project.
Try trimming responseText
before comparing:
xhttp.onload = function(){
var responseText = xhttp.responseText.trim();
alert(responseText); //Here comes "true" or "false" just like I want it
if(responseText == "true"){ //This always gives me "ERROR and then the responseText (which is true or false)
alert("TRUE");
}else if(responseText == "false"){
alert("FALSE");
}else{
alert("ERROR" + responseText );
}
};