Skip navigation.
Home (is where the heart is)
live to play. play to live.

GET, POST and those pesky CHECKBOXes

Chapter | Code

One of the most stultifying, heartburn-inducing stupidities in the HTML/HTTP/CGI specifications is the behavior of the input type="CHECKBOX" form element.

Unlike ANY other form element, the checkbox element name appears in the GET/POST stream ONLY if it has been checked. If it's unchecked it's not present at all, unless you add logic to your code to forcibly include "checkboxelemname=0" or somesuch in your POST.

To complete our calamitous kludge integrating JSP within a PHP framework, of course, we'll have to do just that. Since we saw earlier that we need to convert all POST data to GET so it can "reach" the JSP script processed by the PHP virtual function, the only way to preserve expected checkbox behavior is to keep the element present, set to '1' (or 'checked' or whatever you like) when it's checked, and to '0' (or 'unchecked' or even an empty string) when it's not.