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″.
to see if a checkbox is checked, there’s this function:
http://codex.wordpress.org/Function_Reference/checked
hi devin, i’m curious if it is possible to set a checkbox as true by default and yet still tell when it has been unchecked. is it possible to tell whether no value has been set yet for the field? i seem to be stuck on it returning false in this instance as well as when the value is actually false.
The checkbox will always return a value (0 or 1). If there’s no value set when the options are saved (e.g. unchecked), it gets saved as 0. This is purely because a checkbox won’t get sent in the $_POST data if is unchecked. Trickly little bastards they are: http://wptheming.com/2011/05/checkboxes-and-booleans/
i guess i was wondering if there was a way to check if it was neither 0 nor 1 – as in it hadn’t been set at all yet. very tricky little buggers indeed.
Sorry for commenting twice. It seems my code does not appear. You can see my code here: http://pastebin.com/dBfBMeV6
When I checked or unchecked the output is “unchecked”. Both checked and unchecked checkbox had the same output.
What’s the proper code should I use?
Thank you.
You’re checking for the string “false”. I’d just do: if ( get_option(“example_checkbox2″) ). Or == false (no quotes).