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.

Don P
  • 60,113
  • 114
  • 300
  • 432
  • 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.) – nnnnnn Feb 05 '13 at 21:00
  • 1
    Yes, variables are posted as strings. Testing for the string "false" is always true. – showdev Feb 05 '13 at 21:01
  • 1
    n is right. All GET/POST parameters are strings, and `"false"` is a truthy value. – Fabrício Matté Feb 05 '13 at 21:02
  • Possible duplicate of [boolean variables posted through AJAX being treated as strings in server side](https://stackoverflow.com/questions/3654454/boolean-variables-posted-through-ajax-being-treated-as-strings-in-server-side) – Adam Jun 29 '19 at 10:11
  • A good answer might be found here: https://stackoverflow.com/a/20463398/2311074 – Adam Jun 29 '19 at 10:13

3 Answers3

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.

Jeff Davis
  • 4,736
  • 4
  • 38
  • 44
  • 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? – Don P Feb 05 '13 at 21:03
  • Your answer is correct and works by the way, just need 8 minutes to accept :) – Don P Feb 05 '13 at 21:03
  • @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. – Jeff Davis Feb 05 '13 at 21:06
  • 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. – Beetroot-Beetroot Feb 05 '13 at 21:29
5

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

data : JSON.stringify(json)

and decode it on server:

$data = json_decode($_POST);

Ihor Smirnov
  • 51
  • 1
  • 1
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 
}
SGAmpere
  • 21
  • 3