Checkboxes are one of the more difficult form elements to deal with. For one, checkboxes don’t get sent in $_POST request unless they are checked. Any other element, like a text input or a radio button, will get sent regardless of it’s state. But if someone were to uncheck an option form and send it there would be no indication in the $_POST request that its state had changed.
The way to get around this, or the way that I did, is to automatically assume all checkboxes in a form are unchecked unless your $_POST data explicitly tells you otherwise. In the Options Framework this required an extra step as I was looping through my options array and validating the data from the $_POST:
function optionsframework_validate($input) {
if (!empty($_POST['update'])) {
// Get the options array defined for the form
$options = optionsframework_options();
foreach ($options as $option) {
$id = $option['id'];
// Set the check box to "0" by default
if ( 'checkbox' == $option['type'] && ! isset( $input[$id] ) ) {
$input[$id] = "0";
}
In the above code I’m setting any checkboxes that weren’t set in the form (and therefore weren’t checked) to “0”, and combining with all the other form data ($input) that was sent in the $_POST request. That way I have a complete set of checkboxes available in my $input rather than just the ones that were checked.
How to Store Checkbox State in the Database
The other main issue I had was how to store the checkbox data in the database. In the early versions of the Options Framework I saved checkbox data as either “true” or “false”. This was fine as long as I always evaluated the option (e.g. if ( get_option(“checkbox”) == “false”) ) { do something }).
However, this caused a problem if I wanted to call the option directly, because if “checkbox” is set to “false”, get_option(“checkbox”) returns true. Confused?
“False” (notice the quotes) is a string not a boolean, and most strings return true. If the option “checkbox” was set to “pancake”, that would also return true.
This little script checks 11 values and gives their boolean result:
$booleancheck = array(1,0,-1,"1","0",false,true,'false','true',"randomstring","");
echo '<ul>';
foreach ($booleancheck as $key) {
echo '<li>'. $key.': ';
if ($key) {
echo "True";
}
else {
echo "False";
}
echo '</li>';
}
echo '</ul>';
You’ll see that “0”,”1″ will return false and true respectively regardless of whether they are sent as a string or an integer- and that’s the way I eventually decided to save checkbox data.
I also should have studied the WordPress core options a little earlier because that’s how it does it. If you pull up /wp-admin/options.php of your WordPress site you’ll see all check boxes either saved as “0” or “1”.