12

I'm sending some data in an Ajax call. One of the values is a boolean set to FALSE. It is always evaluated as TRUE in the PHP script called by the Ajax. Any ideas?

$.ajax({
    type: "POST",
    data: {photo_id: photo_id, 
           vote: 1, 
           undo_vote: false},   // This is the important boolean!
    url: "../../build/ajaxes/vote.php",
    success: function(data){
        console.log(data);
    }
}); 

In vote.php, the script that is called in the above Ajax, I check the boolean value:

if ($_POST['undo_vote'] == true) {
    Photo::undo_vote($_POST['photo_id']);
} else {
    Photo::vote($_POST['photo_id'], $_POST['vote']);
}

But the $_POST['undo_vote'] == true condition is ALWAYS met.

5
  • 5
    Have you tried if ($_POST['undo_vote'] == 'true') {? (I'd expect request parameters to be received as strings unless you pass a string of JSON and parse it server-side before testing the individual properties.) Commented Feb 5, 2013 at 21:00
  • 1
    Yes, variables are posted as strings. Testing for the string "false" is always true. Commented Feb 5, 2013 at 21:01
  • 1
    n is right. All GET/POST parameters are strings, and "false" is a truthy value. Commented Feb 5, 2013 at 21:02
  • Possible duplicate of boolean variables posted through AJAX being treated as strings in server side Commented Jun 29, 2019 at 10:11
  • A good answer might be found here: stackoverflow.com/a/20463398/2311074 Commented Jun 29, 2019 at 10:13

4 Answers 4

15

A post is just text, and text will evaluate as true in php. A quick fix would be to send a zero instead of false. You could also put quotes around your true in PHP.

if ($_POST['undo_vote'] == "true") {
    Photo::undo_vote($_POST['photo_id']);
} else {
    Photo::vote($_POST['photo_id'], $_POST['vote']);
}

Then you can pass in true/false text. If that's what you prefer.

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

4 Comments

Is it bad practice to send booleans as 0 or 1? Should I be doing some JSON encoding, or something else to make the type stronger?
Your answer is correct and works by the way, just need 8 minutes to accept :)
@DonnyP It's really a matter of taste. Most programmers know how 1 and 0 work for true/false, but depending upon where this is being used it may be clearer to use JSON.
The traditional (pre-JSON, pre-REST) approach would be to include undo_vote: 1 for true and to exclude undo_vote altogether for false. Then, server-side booleanize $_POST['undo_vote'] depending on whether it's present or not (and optionally interpret '0' and 'false' as false). For something simple like this I might still use that approach today.
5

You can use JSON.stringify() to send request data:

data : JSON.stringify(json)

and decode it on server:

$data = json_decode($_POST);

Comments

2

You can use 0 and 1 for undo_vote and type cast it in php:

JS side:

undo_vote: 0 // false

Server side:

$undovote = (bool) $_POST['undo_vote']; // now you have Boolean true / false

if($undovote) {
    // True, do something 
} else {
   // False, do something else 
}

Comments

0

An old question, but maybe this is useful for Javascript programmers:

I do not want to stringify everything, since I want numbers and booleans without quotes, strings with one pair of double quotes, and objects as they are in my request body.

JQuery ajax did not accept pure boolean values as data body, so I did:

    if (jQuery.type(data) == 'boolean') {
        data = JSON.stringify(data);
    }

Works for me...

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.