0

I'm really confused on this one. I'm trying to do a simple ajax request, and I have php executing 'echo "true";' if the ajax request is successful, or return a string containing the error if it isn't. I'm using this exact technique elsewhere in the code and it compares as expected (msg != "true") { //do something }. The first function I'll post below works with that method, the second doesn't (also I am checking with Firebug the response is "true", and I've done console.log(typeof msg) to verify it is of type string).

This works as expected (the php echoes "true", and it executes show_step2()):

function step2_register() {
  var values = $('.signup .txt').serialize();

  $.ajax({
    type: "POST",
    url: base_url+"login/create_account",
    data: values,
    success: function(msg) {
      if (msg != "true") {
        $("#login_form.signup .error_container #error1").html(msg).css("visibility", "visible");
      }
      else {
        show_step2();
      }
    },
    error: function(error) {
      alert(error);
    }
  });
}

This doesn't work (the php echoes "true", but the js always executes the slideDown() part of the code):

    function register() {
  var data = $("#login_form .input").serialize();

  $.ajax({
    type: "POST",
    url: base_url+"login/register",
    data: data,
    success: function(msg) {
      if (msg != "true") {
        $("#login_form.signup #error2").html(msg).slideDown();
      }
      else {
        show_success();
      }
    },
    error: function(error) {
      alert(error);
    }
  });
}

What's going on here?

3
  • check the content type of the return message or set it to text/plain. Commented Aug 2, 2011 at 6:46
  • 2
    Do this, alert('_' + msg + '_'); check if there are any spaces that have been induced by PHP, the underscores help you see any whitespace before and after the msg string. If you find whitespace, consider using .trim() (api.jquery.com/jQuery.trim) to remove them. Commented Aug 2, 2011 at 6:49
  • Thanks Ben, that helped alot. I traced it down to an extra line after the closing php tag (?>) in a model I was loading in the ajax function. Looks like I need to improve my debugging skills ;) Commented Aug 3, 2011 at 2:54

1 Answer 1

2

Your php is likely outputting something else invisible like a newline character after the word "true". Either make sure you call die(); or exit(); right after outputting "true", (and doing everything else you need), or make sure there are no line breaks in your PHP editor before the <?php and after the ?>.

You can also check that the string begins with "true" instead of equals "true" by trying something like msg.substring(0,4) == "true"

Sign up to request clarification or add additional context in comments.

3 Comments

all good recommendations, though substring won't do much if any junk character is before true..
@Ben True, I think he'd be more likely to have noticed it in Firebug if it was before though. Although I did say to check before <?php just in case didn't I. I should be more consistent haha. Regardless the Javascript solution was an afterthought, fixing it at the server side would be a much better choice :)
You were correct PaulPRO. Had an empty line after the closing PHP tag in a model I was loading in the php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.