0

So I've been testing and found out that if

session_start();
if ( $_POST['animal'] != $_SESSION['animal'] ) die(json_encode(false)); 

is run inside my PHP function to handle the AJAX call, then back in JavaScript land where I have

        success: function (correctCaptcha) {
            console.log("correctCaptcha is " + correctCaptcha); // test
            console.log("correctCaptcha's type is " + typeof (correctCaptcha)); // test
                if ( correctCaptcha )
                {

I'm seeing that correctCaptcha is the string "false" rather than the boolean value false. Consequently, the if block is entered and that's what's causing a bug. How can I get PHP to give me a boolean as the JSON it generates? Or what is a better solution?

2
  • 2
    Simple solution would be if ( correctCaptcha == "false") Commented Dec 9, 2015 at 0:09
  • @RiggsFolly Obviously, but I don't want to jerry-rig the coding of this site too much Commented Dec 9, 2015 at 0:49

1 Answer 1

2

You can do

json_encode((bool)1); // which is true

Or

json_encode((bool)0); // which is false

Ideally though you would want to do something like this

$results = ["result" => false];
json_encode($results);

From the documents, this should retain the boolean, not convert to a string.

Then access the results in your javascript like this (as an object)

console.log(correctCaptcha.result);
Sign up to request clarification or add additional context in comments.

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.