Skip to content
Advertisement

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

JavaScript

The AJAX request processing php part –

JavaScript

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:-

JavaScript

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:-

JavaScript

Finally, if I alter the server side code to this:-

JavaScript

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

Advertisement

Answer

You aren’t doing anything wrong per se, it’s just that when it gets posted, it looks like this:

JavaScript

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:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement