math

Checkboxes and Booleans

Posted

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'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

10 thoughts on “Checkboxes and Booleans

  1. kathy wrote:

    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.

    • Devin wrote:

      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/

      • kathy wrote:

        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. Kharis wrote:

    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.

    • Devin wrote:

      You’re checking for the string “false”. I’d just do: if ( get_option(“example_checkbox2″) ). Or == false (no quotes).

  3. balu wrote:

    hi,,

    can u help me how to store checkbox values in table with specific id.. and i also need to update….

  4. Nathz wrote:

    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

  5. Suresh wrote:

    hi devin,
    can you please tell me were to insert this code?? can i insert it in theme function.php file?

    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>