2

I tested status.php return value with var_dump($result) and alerted it out in check() function like this:

function check() {
    $.ajax({
        url: "status.php"
    }).done(function(data) {
        alert(data);
    });
}

and it did return true or false depending on situation, but when I check if data is true or false inside of check() function it always returns false.

status.php:

<?php 
function status(){
    if(logged() === true) {
        $result = true;
    } else {
        $result = false;
    }
    return $result;
}
status();
?>

check() function: always alerts "false" even though sometimes should be "true"

function check() {
    $.ajax({
        url: "status.php"
    }).done(function(data) {
        if(data === true){
            alert("true");
        } else {
            alert("false");
        }
    });
}
1
  • I doubt it return anything because there is no echo in php so nothing will be sendback to browser in ajax request. Commented Apr 23, 2016 at 11:56

6 Answers 6

3

You're not sending the return value of the status() function back to PHP. Use:

echo json_encode(status());

And change the AJAX call to expect a JSON response.

function check() {
    $.ajax({
        url: "status.php",
        dataType: 'json'
    }).done(function(data) {
        alert(data);
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

also note that var_dump or json_encode would be string so won't pass === true comparison
"I tested status.php return value with var_dump($result)"
I assumed that was just temporary debugging, it's gone now.
2

you just echo the $result like this

ajax not returning value so that we have to echo it.

 <?php function status(){
if(logged() === true) {
    $result = true;
} else {
    $result = false;
}
echo $result;  } status();  ?>

and then should be like this

function check() {
$.ajax({
    url: "status.php"
}).done(function(data) {
    if(data == "true"){
        alert("true");
    } else {
        alert("false");
    }
});   }

Comments

1

Use

**dataType: 'json'** 
function check() {
    $.ajax({
        url: "status.php",
        dataType: 'json'
    }).done(function(data) {
        alert(data);
    });
}

and on status.php use

echo json_encode(status()); 

Comments

1

You cannot get the response by return method in ajax. to get value "echo" whatever the result in the function , like

function status(){
    if(logged() === true) {
        $result = "1";
    } else {
        $result = "0";
    }
    echo $result;exit;
}

you will get the value 1 or 0 in your ajax success function

Comments

0

Ajax may not be returning a boolean true or false, rather a string.

So try and put true in double quotes:

if(data=="true")

You can also use the trim function on data to ensure no whitespace is present in the returned data, like so:

if($.trim(data)=="true")

2 Comments

The AJAX isn't returning anything, since there's no echo in the PHP.
Thanks for pointing it out. You can use echo $result instead of $return result in status.php.
0

Just Remove Type checking i.e '===' replace with '=='

function check() {
    $.ajax({
        url: "status.php"
    }).done(function(data) {
        if(data == true){
            alert("true");
        } else {
            alert("false");
        }
    });
}

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.