Checkboxes and Booleans

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”.

About Devin

I am a developer based in Austin, Texas. I run a little theme shop called DevPress and help manage a WooCommerce shop with Universal Yums. Find me on twitter @devinsays.

10 Responses

  1. 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.

      1. 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.

  2. Nathz

    how to show post under category that selected by Multicheck ?
    I try use code like this,
    $multicheck = of_get_option( ‘featured_category’, ‘default’ ); print_r($multicheck);
    $args= array(‘cat’ => $multicheck, ‘showposts’=> 3); query_posts($args);
    while ( have_posts() ) : the_post();

    but I get error like this
    Array ( [11] => 1 [12] => 1 [13] => 1 [14] => 0 [16] => 0 [21] => 0 [22] => 0 [37] => 0 [46] => 0 [1] => 0 )
    Warning: urldecode() expects parameter 1 to be string

Leave a Reply