SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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).

    Code JavaScript:
    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
    Follow the dream, don't chase the competition.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You could use parseFloat() to turn the string into a numeric value.

    Code JavaScript:
    var response = parseFloat(ajaxRequest.responseText);
     
    if (response) {
        alert("Success");
    } else {
        alert("Failed");
    }

  3. #3
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You win my jar of cookies

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

    Thank again
    Follow the dream, don't chase the competition.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  5. #5
    SitePoint Zealot Steveiwonder's Avatar
    Join Date
    Nov 2008
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird

    Anyways, thanks dude.
    Follow the dream, don't chase the competition.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Steveiwonder View Post
    Weird
    It can be weird, but further investigation can help to determine the cause, such as PHP outputting a newline after the value, for example.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •