Here is some code that shows what happens with checkboxes.
The important point about them is that UNCHECKED ones DO NOT SEND A VALUE in $_POST! Only CHECKED ones do. I supply some code that demonstrates the point.
The 'var_dump' of $_POST allows you to see what really comes back from the form for 'checkboxes'.
It is tested code.
It works on PHP 5.3.18 on windowx XP.
<?php
// we will have five checkboxes and each will have a separate value as follows...
$checkboxValues = array('cbx01', 'cbx02', 'cbx03', 'cbx04', 'cbx05');
// This array keyed on the checkbox VALUE will be checked or not...
// This is ALL the checkboxes!
$checkboxIsChecked = array('cbx01' => false,
'cbx02' => false,
'cbx03' => false,
'cbx04' => false,
'cbx05' => false);
/*
* Determine the 'cat' checkboxes that are actually currently checked!
*
* We can know this because the $_POST['cat'] array
* will contain VALUES for the Checkboxes that the user ACTUALLY checked!
*
* The UNCHECKED Checkboxes do NOT send any VALUE so they are MISSING from the array!
*
* i.e. if checkbox value: 'cbx02' and 'cbx04' are checked by the user then
* the 'cat' array will be :
* 0 => 'cbx02'
* 1 => 'cbx04'
*
* All the other checkboxes will be 'unchecked'.
*
*
*/
var_dump($_POST); // you can see what comes in!
if (isset($_POST['cat'])) { // may have some checked 'cat' checkboxes...
foreach ($_POST['cat'] as $checkboxValue) {
// mark the appropriate checkbox as 'CHECKED' i.e cbx03
$checkboxIsChecked[$checkboxValue] = true;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Show Checkboxes and the input from them.</title>
</head>
<body>
<div class="main" id="main">
<!-- heading -->
<strong><?php echo 'Test checkbox setting...'?></strong><br/>
<form method="POST" action="">
<?php foreach($checkboxValues as $cbxValue): ?>
<div>
<?php $labelId = $cbxValue . '_id'; // generate a label ?>
<label for="<?php echo $labelId?>"><?= ucwords('A '. $cbxValue . ' thingy!') ?></label>
<input type="checkbox"
name="cat[]"
id="<?php echo $labelId?>"
value="<?php echo $cbxValue?>"
<?php echo $checkboxIsChecked[$cbxValue] ? 'checked' : '' ?>>
</div>
<?php endforeach; ?>
<input type="submit" value="GO"/>
</form>
</div>
</body>
</html>