Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boolean variables posted through AJAX being treated as strings in server side

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

like image 573
Sandeepan Nath Avatar asked Sep 06 '10 21:09

Sandeepan Nath


People also ask

Are Boolean values strings?

The string representation of a Boolean is either "True" for a true value or "False" for a false value. The string representation of a Boolean value is defined by the read-only TrueString and FalseString fields. You use the ToString method to convert Boolean values to strings.

What is a string Boolean?

A Boolean search string is a search tool that allows you to limit or require specific results. Recruiters can use it to find resumes and candidates that most closely match the required qualifications. If you're a recruiter, learning more about Boolean search strings can help you make your search process more efficient.

Which object use in Ajax to get data from server?

The XMLHttpRequest object is used to request data from a server.

What type of object does Ajax return?

The $.ajax() function returns the XMLHttpRequest object that it creates.


2 Answers

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); 
like image 81
Tamara Avatar answered Sep 20 '22 14:09

Tamara


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 } 
like image 35
Nick Craver Avatar answered Sep 19 '22 14:09

Nick Craver