1

I have this:

$(document).ready(function() {

$.get('getturn.php', function(data){
    if (data!=='4') { alert ('  change'); }
    if (data=='4') { alert ('no change');}
    });
});

getturn.php echoes 4 if the turn is equal to a session id, and it echoes the turn number if otherwise. The getturn.php does as it should, it echoes 4 or a number like 0,1,2,3; however when I get the data like seen above from a different file, and check if it equals to 4, I can't get the correct answer... What am I doing wrong? I thought we could check if the output was yes with data=='yes' ? but we can't check numbers with data=='4'?

3
  • please paste your php code here too? also what happens if you do console.log(data); ? Commented Jun 7, 2011 at 13:36
  • Did you check the response via firebug. may be your ajax request is cached. Commented Jun 7, 2011 at 13:37
  • @GaryGreen data type is a string but when i make it data='4' and data!='4' it alerts both change and no change Commented Jun 7, 2011 at 13:40

2 Answers 2

2

Have you done an alert(data); to see what is returned?

I think you should try this slight modification:

if (data == '4') { alert ('no change');}
            else { alert ('  change'); }

Based on this answer: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

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

5 Comments

@Frosco hmm... for somereason localhost/getturn.php echoes 4 but when i alert it it alerts 1. still the type is a string though..
@Logan the first part of your response cancels out the second part. How are you sure that it echoed a 4? Unless you're using your browser dev tools and inspecting the real response how can you know?
@Frosco well localhost/getturn.php returns only number 4 and nothing else... isn't that enough for checking the php file? Why should it be different when I use ajax get? Could you enlighten me please?
@Logan I don't know how else to explain, if alert(data) shows you 1 what does that tell you?
nevermind i found the problem in the php script; after it alerted 4 i did as in your answer and it worked, thanks.
1

if understand your problem correctly, this will help:

data = parseInt(data);
if (!isNaN(data)) {
  if (data != 4) { ... }
  if (data == 4) { ... }
}

just convert "data" variable to numeric value(e.g. integer in this case)

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.