3

Probably some simple derp but I just can't figure this out, I'm using almost identical code in a different solution and it works.

Javascript:

$('#drop').click(function(e) {
    e.preventDefault();
    $('#login').slideToggle(1000);
});

$('#sub').click(function(e) {
    e.preventDefault();
    $('#info').html('<img src="img/load.gif" />')
    $.post('login.php', {user: document.getElementById('user').value, pass: document.getElementById('pass').value}, function(response) {
        if (response == 'ok')
        {
            $('#login').slideUp(500);
            $('#info').html('');
            $('#joocy').html('<img src="img/load.gif" />').load('manage.php');
        }
        else if (response == 'error')
        {
            $('#info').html('Invalid user/pass');
        }
    });
});

PHP:

if ($row = mysql_fetch_array($result))
{
    echo 'ok';
}
else
{
    echo 'error';
}

Response is being sent back but the if-clause isn't catching it. typeof() returns string.

EDIT: else if (response === 'error ') // notice the space in the string

This works, can anyone explain why the space is appended to the returnstring?

SOLVED: There was a space after PHP closing tag. Thanks for your time everyone! :)

8
  • what does the response var return, and what does the if clause not do? Returns false always? Commented Sep 29, 2011 at 6:12
  • Returns ok or error as expected and if-clause returns false. Commented Sep 29, 2011 at 6:19
  • you are missing a semi-colon after $('#info').html('<img src="img/load.gif" />') Commented Sep 29, 2011 at 6:23
  • 1
    You should provide the rest of the PHP file (especially after the else { echo 'error'; }) since a whitespace character after the PHP close tag (?>)might be sent along with the text. Commented Sep 29, 2011 at 6:33
  • 1
    I know this isn’t what you’re after, but you could make your life a lot easier if you would have your server respond with a proper response code and use jQuery's response handlers error and success. Commented Sep 29, 2011 at 6:40

3 Answers 3

2

You probably have a space after the closing '?>' tag in your php script. Try either removing all whitespace after, or the closing tag altogether.

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

Comments

1

Maybe the output contains other characters like e.g. a BOM.
What does

$.post('login.php', {user: document.getElementById('user').value, pass: document.getElementById('pass').value}, function(response) {
    if (response == 'ok')
    {
        $('#login').slideUp(500);
        $('#info').html('');
        $('#joocy').html('<img src="img/load.gif" />').load('manage.php');
    }
    else if (response == 'error')
    {
        $('#info').html('Invalid user/pass');
    }
    var tmpText = "" + response.length + ": ";
    for(var tmpI=0; tmpI<response.length; tmpI++) {
        tmpText += response.charCodeAt(tmpI) + " ";
    }
    $("<fieldset></fieldset>").text(tmpText).appendTo("#info");
});

print?

Comments

0

may the additional space is added from the php side of the code, you can use trim

if($.trim(response)==='error')
{
 //
}

Comments

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.