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'?

Logan
  • 10,649
  • 13
  • 41
  • 54

2 Answers2

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?

Community
  • 1
  • 1
Fosco
  • 38,138
  • 7
  • 87
  • 101
  • @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 Jun 07 '11 at 13:44
  • @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? – Fosco Jun 07 '11 at 13:46
  • @Frosco well http://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 Jun 07 '11 at 13:50
  • @Logan I don't know how else to explain, if alert(data) shows you `1` what does that tell you? – Fosco Jun 07 '11 at 13:56
  • nevermind i found the problem in the php script; after it alerted `4` i did as in your answer and it worked, thanks. – Logan Jun 07 '11 at 14:01
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)

ioseb
  • 16,625
  • 3
  • 33
  • 29