boolean variables posted through AJAX being treated as strings in server side
Solution 1
You aren't doing anything wrong per se, it's just that when it gets posted, it looks like this:
operation=add_cart&isClass=true&itemId=1234
PHP can't tell what the data type is because it isn't passed, it's always just a string of POST data, so compare it to "true"
to do your checks, like this:
if($_POST['isClass'] === "true")
{
//Code to add class to session cart
}
else
{
//Code to add pack to session cart
}
Solution 2
Also you can use filter_var function with filter FILTER_VALIDATE_BOOLEAN. According to php documentation it
Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise. If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.
So receiving of POST parameter will look like:
$isClass = filter_var($_POST['isClass'], FILTER_VALIDATE_BOOLEAN);
Solution 3
This is a bit of an old question, but I'm surprised nobody has posted this here as solution.
Just use 1 and 0 instead of true and false when you're constructing your ajax requests.
When you do a ==
comparison, they'll be interpreted as true/false.
JS:
$.ajax({
url: '....',
data: {
foo: 1,
bar: 0
}
});
PHP:
<?php
if ($_GET['foo']) {
//...
} else {
//...
}
echo $_GET['bar'] ? 'bar is true' : 'bar is false';
?>
Solution 4
- Send the data from your javascript as stringified JSON.
- Make a PHP function to convert the strings 'true' and 'false' to boolean value.
Personally I like #2, which goes with Nick Craver's answer.
![Sandeepan Nath](https://i.stack.imgur.com/01yQM.png?s=256&g=1)
Sandeepan Nath
Organizer of meetup group PHP Micro Meetups (Pune) About me I am a Software Developer/Designer from India. Here is my Brief Résumé on Stackoverflow Careers. Contact me at sandeepan (dot) nits (at) google's mail. Other than programming I have deep interests in Human Psychology, Entrepreneurship, Social work and Scientific Research. While younger I had deep interests in Genetics, Astronomy etc. I love computer games, especially FPS games on multi-player, cricket, music, racing and adventure. I am a 24 years old guy and as time is passing by, I am gradually coming to realize that there are so many things to see and do in this beautiful world. I am afraid that a few years down the line I will repent over so many things I wished to do but could not do. However, I have started serious planning for all this.
Updated on July 25, 2022Comments
-
Sandeepan Nath almost 2 years
Following is a part of an AJAX functionality to add classes and packs to session cart:-
The jquery part
function addClassToCart(itemId) { addItemToCart(itemId,true); } function addPackToCart(itemId) { addItemToCart(itemId,false); } function addItemToCart(itemId,isClass) { $.post(url+"/ajax/add_cart", { operation: 'add_cart','isClass':isClass, 'itemId': itemId}, function(data) { if(data.success) { alert("item added to cart"); } }, "json"); }
The AJAX request processing php part -
//Checking operation and other posted parameters if($_POST['isClass']) { //Code to add class to session cart } else { //Code to add pack to session cart }
The strange thing
No matter whether I pass true/false (by calling addClassToCart() and addPackToCart()), always the code to add class to session cart executes.
If I put echo statements there like this:-if($_POST['isClass']) { echo "see if condition ".$_POST['isClass']; } else { echo "see else condition ".$_POST['isClass']; }
This is the output:-
addClassToCart()
see if condition true
addPackToCart()see if condition false
Putting conditions like this in the jquery code however works fine:-
function addItemToCart(itemId,isClass) { if(isClass) alert("is class"); else alert("is pack"); }
Finally, if I alter the server side code to this:-
if($_POST['isClass'] === true) { echo "see if condition ".$_POST['isClass']; } else { echo "see else condition ".$_POST['isClass']; }
These are the outputs -
addClassToCart()
see else condition true
addPackToCart()see else condition false
So, why is the boolean variable treated as a string here? Am I doing something wrong in posting parameters?
Thanks, Sandeepan
-
Sandeepan Nath almost 14 yearsSo is there no other way around? I prefer doing boolean checks as much as possible.
-
Nick Craver almost 14 years@sandeepan - nope, not sure what else to say other than...this is how http gets and posts work, it's perfectly normal.
-
Gary about 12 yearsWhy not instead use '==' and let it do type conversion for you?
-
Nick Craver about 12 years@Gary - Because it's a string , "false" is still true because it's a non-zero length string...be explicit and you won't get surprises. The same rules apply to JavaScript for example, some people never use
==
and only use===
for good reason. -
Gary about 12 yearsI guess I had a misconception of type conversion. Thanks Nick.
-
Hayden Chambers over 10 yearsnumber 1 works far better when the object you want to save is multi-leveled.. no ugly recursive php functions required just json_decode
-
mcrumley over 10 years+1 for
filter_var
, one of the often overlooked parts of the PHP. -
Spock over 9 yearsBeautiful! Validating a boolean like it were a string is imho ugly and fubar.
-
r3wt over 9 yearsyou can type cast it to a boolean like so
$foo = (bool) $_POST['foo'];
-
Spencer Williams over 7 years@r3wt You would need to explicitly test for
'true'
and'false'
and reset those values to booleans. The string'false'
cast as a boolean will be true. Check out us2.php.net/manual/en/language.types.boolean.php to review how PHP converts such values. -
Jonathon Philip Chambers almost 6 yearsAlthough I voted up the top three answers this was the one that was so clean and elegant, it made it into my code. Well done.
-
Lizesh Shakya over 2 yearsThe best solution for retrieving the data in Ajax or Api.
-
Hashim Aziz over 2 yearsPlease get rid of the space between
filter_var
and its parentheses, it makes the code only slightly harder to read but unimaginably disrupting to my internal peace and the system won't let me make such a minor edit. 😬 -
Tamara over 2 years@HashimAziz done! Hope it brought internal peace to you :)
-
Hashim Aziz over 2 years@Tamara If only, but close enough. Haha thanks
-
Boolean_Type about 2 yearsIn case if
$_POST['isClass']
hasn't been passed at all (ie$_POST['isClass']
isNULL
),filter_var($_POST['isClass'], FILTER_VALIDATE_BOOLEAN);
will returnfalse
as well.