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

Erny

New Member
Following is a part of an AJAX functionality to add classes and packs to session cart:-The jquery part\[code\]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");}\[/code\]The AJAX request processing php part -\[code\]//Checking operation and other posted parametersif($_POST['isClass']){ //Code to add class to session cart}else{ //Code to add pack to session cart}\[/code\]The strange thingNo 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:-\[code\] if($_POST['isClass']) { echo "see if condition ".$_POST['isClass']; } else { echo "see else condition ".$_POST['isClass']; }\[/code\]This is the output:- addClassToCart() \[code\]see if condition true\[/code\]
addPackToCart() \[code\]see if condition false\[/code\]Putting conditions like this in the jquery code however works fine:-\[code\]function addItemToCart(itemId,isClass) { if(isClass) alert("is class"); else alert("is pack"); }\[/code\]Finally, if I alter the server side code to this:-\[code\]if($_POST['isClass'] === true) { echo "see if condition ".$_POST['isClass']; } else { echo "see else condition ".$_POST['isClass']; }\[/code\]These are the outputs - addClassToCart() \[code\]see else condition true\[/code\]
addPackToCart() \[code\]see else condition false\[/code\]So, why is the boolean variable treated as a string here? Am I doing something wrong in posting parameters?Thanks,Sandeepan
 
Back
Top