-1

I am screwed with this one the if statement is not working in javascript. I am using js to call php and php file echo 'true';

My java script code

 function like_add(article_id){
$.post('my/like.php', {article_id:article_id}, function(data){
    if(data == 'true'){//this part never works
        alert(data);
    }else{
    alert(data);
    }
});

}

I tried to change alert with alert('*'+data+'*');

And got that there is a space * true*. How can I get this script t work.

Whole code

 function like_add(article_id){
$.post('ajax/like.php', {article_id:article_id}, function(data){

    if(data == 'true'){
        total_votes(article_id);
    }else{
    alert(data);
    }
});

} PHP code

        if(previously_liked($article_id) === true){
        echo 'You\'ve already liked this!';                 
        }else{
            add_like($article_id);
            echo 'true';
        }


function total_votes(article_id){
$.post('ajax/total_votes.php', {article_id:article_id}, function(data){
    $('#article_'+article_id).text(data);

}); }

25
  • Wait ... if the PHP code echoes "success", where does the string with "true" in it come from? Commented Jun 24, 2012 at 13:18
  • Please post your actual code. I've a strong feeling that this is an untested reduced example. And define "not working". Are you saying that neither condition executes? Or is it always the else? Commented Jun 24, 2012 at 13:19
  • @am not i am the if is not working as there is white space before string which is echoed an I have tried all sort of methods but nothing worked!! else is working fine Commented Jun 24, 2012 at 13:29
  • @NavneetPandey: What happens if you do alert(typeof data) before the if statement. Commented Jun 24, 2012 at 13:32
  • 1
    @NavneetPandey: Then try if($.trim(data) == 'true') { Commented Jun 24, 2012 at 13:58

3 Answers 3

2

Well you could include a space in your test:

  if (data == ' true') { ... }

Or you could look for the pattern "true" in the data:

  if (/true/.test(data)) { ... }

The latter would probably be more resilient, though really it'd be better to have your server return something more structured and less ambiguous.

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

2 Comments

Well you're going to have to provide more detail; the regular expression would definitely work.
Can you console.log(data) before the if, just to be sure that data is what you expect it is?
0

If it's a leading space bugging you, try

if (data.replace(/^\s+|\s+$/,'').toLowerCase() === 'true')

Comments

0

I think you can do following:

  1. Check your response that should not contains space. Or
  2. Use data = data.replace(/\s/g, "");

5 Comments

I have tried this. in this also if condition is not working but the alert is without space.
can you try like if(data == true), I mean without single or double qoutes.
I think your callback function is returning ' true' (true with space). If it return 'true' (without space) then it will work. Hope this help.
Use data = data.replace(/\s/g, ""); before if condition.
Debug it in firefox with firebug, if you know how to debug :)

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.