PHP, mass actions and the missing value of a checkbox
Recently I was developing some advanced functionality for WordPress plugin that included mass delete and update by setting on few checkboxes and hitting some Delete or Update button. Retrieving values from post in PHP and executing actions based on the existence of that value is easy, all you need to do is something like
1 2 3 4 5 6 | /* This will tell you if the button of a type submit with the name “someNameValue” assigned to it has been clicked */ if(isset($_POST['someNameValue'])) { # DO SOME ACTION } |
Where “someNameValue” can come from let’s say:
1 | <input id="someIdValue" class="someClassValu" type="submit" name="someNameValue" /> |
All you need to do is to set your form method to POST and direct action to corresponding PHP file.
Let’s suppose we want to see if some checkbox was checked. We would have something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Single checkbox sample</title> </head> <body> <?php if($_POST['submitUpdate']) { $o = '<p>You clicked the SUBMIT button</p>'; echo($o); } if($_POST['someCheckBoxName']) { print_r($_POST['massUpdate']); } ?> <form name="sampleForm" action="" method="POST"> <input type="checkbox" name="someCheckBoxName" value="someValueGoesHere" /> <br /><br /> <input type="submit" value="Submit checkboxes" name="submitUpdate" /> </form> </body> </html> |
Ok, this was a simple example. Most of our day to day tasks, especially the ones in programming advanced features are not going to be simple as that.
Lets suppose we have a list of some names, addresses, and other useful information we retrieve from database. Let’s say all hour Names have one column that can be represented by checkbox, something like user is active or inactive.
Following example represents a list like that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Single checkbox sample</title> </head> <body> <?php if($_POST['submitMassUpdate']) { $o = '<p>You clicked the SUBMIT button</p>'; echo($o); } if($_POST['someCheckBoxName']) { # print_r will now throw out the array # because someCheckBoxName[] stands for array print_r($_POST['massUpdate']); $o .= '<ul>'; foreach($_POST['someCheckBoxName'] as $singleBoxValue) { $o .= '<li>box value: '. $singleBoxValue .'</li>'; } $o .= '</ul>'; echo($o); } ?> <form name="sampleForm" action="" method="POST"> <input type="checkbox" name="someCheckBoxName[]" value="someValueGoesHere" />User1 <input type="checkbox" name="someCheckBoxName[]" value="someValueGoesHere" />User2 <input type="checkbox" name="someCheckBoxName[]" value="someValueGoesHere" />User3 <br /><br /> <input type="submit" value="Submit checkboxes" name="submitMassUpdate" /> </form> </body> </html> |
So far I’ve showed you a simple and array checkbox. Now we come to the missing feature of an checkbox, dual state. Checkbox returns only value if it is checked, if not, it returns null. This might seem ok at first, but what if you need to do active/inactive then update on your user list. Let’s say you’re storing database table id values in checkbox, and when the checkbox is checked and submit button click you want to perform update on the database based on the status of those checkboxes. The thing is you will be able to perform updates on checked checkboxes but not on unchecked checkboxes because $_POST will only retrieve value attributes of checked checkboxes.
If you do something like the example that follows, it wont work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | if(isset($_POST['submitMassUpdate'])) { if(isset($_POST['someCheckBoxName'])) { foreach($_POST['someCheckBoxName']) as $singleCheckBoxValueField) { # do some query on database using # singleCheckBoxValueField that is storing some database table ID global $wpdb; $sql = "UPDATE `wp_dealers` SET `dm_status` = '1' WHERE `wp_dealers`.`id`= . $singleCheckBoxValueField ; $wpdb->query($sql); } } else { # do some other query on database using unchecked # singleCheckBoxValueField that is storing some database table ID global $wpdb; $sql = "UPDATE `wp_dealers` SET `dm_status` = '0' WHERE `wp_dealers`.`id`=”.$singleCheckBoxValueField ; $wpdb->query($sql); } } |
The above example wont work cause in the else statement $singleCheckBoxValueField is always null. I believe much more flexibility would be achieved if only checkboxes were returning values for both states, checked and unchecked like radio buttons do.
Problem above can be solved using somewhat different query on the database.
If we were to use only if statement and write our mysql query like in the following example, then we would solve our problem. Notice the IN and NOT IN statements inside the query. This way we are updating our entire database column and setting values to active or inactive based only on checked checkboxes.
1 2 3 | $sql = "UPDATE `wp_dealers` SET `dm_status` = '1' WHERE `wp_dealers`.`id` IN (". $id .")"; $sql2 = "UPDATE `wp_dealers` SET `dm_status` = '0' WHERE `wp_dealers`.`id` NOT IN (". $id .")"; $wpdb->query($sql); $wpdb->query($sql2); |
Example above has one bug in it. If the checkbox list we are working on containes no initial checkboxes checked, then the above if statement will be skipped and no update (to set database column state ti inactive) will be performed.
If you’re confused by the $wpdb->query($sql2), don’t be. It’s a WordPress function to query the database. Hope this was helpful.

