Java, Ajax, PHP string comparison phail!

Hey all,

Been a while since i posted here so, hai again.

This is weird to me, dont under stand it.

I am new to Java and Ajax but PHP is my best friend.

Basically, i have some Ajax that checks a PHP script and when done, the PHP Script will echo either 0 or 1 (success or fail).

This is the code (not alot to see).


var response = ajaxRequest.responseText;
                        
if(response == "1"){
  alert("Success");
}else{
  alert("Failed");
}

There is more to the code, but thats simplfied and yeilds the same result.

If i do alert(response); it shows either a 1 or 0.

However when i do the If(response == “1”) etc it always defaults to the else part of the statment even when the response var == 1.

I cannot seem to get any string comparison to work.

I have tried a few things like trimming white space but i’m not getting anywhere.

Thanks for any help.

Steve

You could use parseFloat() to turn the string into a numeric value.

var response = parseFloat(ajaxRequest.responseText);

if (response) {
    alert("Success");
} else {
    alert("Failed");
}

You win my jar of cookies :slight_smile:

It works, but i would like to understand why my solution didn’t work as it should of.

Thank again

No problem, your in the same vote as me in understanding why sometimes a value works and sometimes it doesn’t. At times i even have trouble getting a value to work correctly via JS and PHP scripts because the typeof seems to change half way through.

Weird :smiley:

Anyways, thanks dude.

It can be weird, but further investigation can help to determine the cause, such as PHP outputting a newline after the value, for example.