0

I'm using AJAX with PHP for a signup page.

If the signup is successful, signup.php will :

echo "success";

The following AJAX code processes this:

if(ajax.responseText != "success"){//if the php file does not echo "success"
    document.getElementById('status').innerHTML = ajax.responseText;//AJAX response is displayed in a div with id "status"

    } else {//if signup is successful
      window.scrollTo(0,0);
      document.getElementById("signupform").innerHTML = "OK. Check your email inbox and junk mail box in a moment to complete the sign up process by activating your account.";
    }
}

Here's my question: ajax.responseText //has a value of "success" but (ajax.responseText != "success") returns true.

Phil
  • 157,677
  • 23
  • 242
  • 245
raneshu
  • 363
  • 2
  • 16
  • 46
  • 2
    there might be some unseen whitespace before/after. try using [`.trim()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim) -> `if(ajax.responseText.trim() != "success")` – Sean Oct 22 '14 at 03:32
  • Try `(ajax.responseText !== "success")` – Sebastian Osuna Oct 22 '14 at 03:32
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Paul Oct 22 '14 at 03:32
  • 2
    There is a classic mistake that can be made here. It is described in the above link "How to return the response from an Ajax call?" Could you show some more code? We would need to see the enclosing function of the block you posted and where the ajax call is actually done. – Paul Oct 22 '14 at 03:34
  • Further to sean's remark, properly placed `console.log(ajax.responseText)` might be insightful as well. – Paul Oct 22 '14 at 03:36
  • Thanks. Adding .trim() worked! – raneshu Oct 22 '14 at 03:37
  • 1
    Well if trim worked it probably wasn't what I was thinking... – Paul Oct 22 '14 at 03:39

0 Answers0