Obviously, you include whatever other fields you have in the table, and perform appropriate checking / escaping where relevant. It also assumes you have opened a DB connection already.
I usually use integers for yes no values and translate them if needed on output for display. In that case, you could still use the hidden field, but if the value wasn’t present it would still register as 0 or no when it was type cast ($choice = (int) $_POST[‘checkboxname’]). The hidden field method is especially nice if you have a lot of checkboxes and need to keep track of the numerical order regardless of which one is checked and which one isn’t.
Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.
In the example of the hidden field, the value that is transmitted is the last non blank value in source code order. Therefore if the checkbox is not checked what comes through is the value of the hidden field. If the checkbox is checked, then it is last in source code order and will have it’s value sent.
You can however use array syntax with any kind of form field.
Any of those will work, and bring back an array to PHP. In fact if you want to use a multiple select list, you have to use array syntax for the name, in order for PHP to receive all the selected items.
Keep in mind, that Javascript doesn’t look at the array syntax in the name, only PHP. To javascript the name attribute is a literal string, regardless. Items named the same thing are part of a named node map, but naming a form control…
I always pass a numeric value in the checkbox field as Hammer65 has shown:
<input type="checkbox" name="active" value="1" />
Then since checkboxes don’t pass any value (they are not set) if they aren’t checked, it’s easy to test for on the backside with isset():
$active = isset($_POST['active']) ? 1 : 0;
The you save the NUMERIC value into your database in a TINYINT(1) field for MySQL (for other databases, you can use a proper BOOLEAN column type). Any other time you need to test the value, you can just put the variable from your database into an ‘if’ statement, and if becomes very easy since 0 evaluates as false:
if($record->active) {
// do stuff
}
Numeric 1/0 values are easier to store and understand across languages, easier to test for, and require less disk space to store than text. Always use numeric 1/0 values for simple on/off stored data.
A common error some newbies make in their forms is to give several fields the same name without using an array and then wonder why only the last of those fields actually gets passed. The use of a hidden field with the same name as a checkbox is one of the few instances where giving two fields the same name is meaningful - the only other one I can think of is radio buttons. In each case only one value will be passed with that value decided by which radio button is selected or whether the checkbox is or isn’t checked. The only other time it would work for multiple fields having the same name without using an array is if you use JavaScript to control which of them are actually in the form.
I think it would be more efficient to process the checkbox on the other side, rather than have to send out an extra hidden field every time, but ah well