Bool parameter from jQuery Ajax received as literal string "false"/"true" in PHP Bool parameter from jQuery Ajax received as literal string "false"/"true" in PHP php php

Bool parameter from jQuery Ajax received as literal string "false"/"true" in PHP


In my experience if you have

dataType: 'json'

and

contentType: 'application/json'

in your jQuery ajax call, and JSON.stringify your data before you send it will arrive at the server as a parse-able javascript object. Or in my case I'm using NodeJS serverside and it arrives as a Javascript object, with correct booleans, not string bools. But if I leave out the content type attribute then things get all goofed up including the bools.


Because HTTP is a text protocol with no concept of booleans or integers everything must be stringified. Comparing strings on the server side is the normal way to deal with booleans.

If you really really want PHP to see a 0 or 1 you can use

 valueTrue : true ? 1 : 0

in your AJAX call.


I ran into the same issue. I solved it by sending a 1 or 0 for booleans.

In the $.ajax I did data : {field_name: field ? 1 : 0}