The problem is you cannot loop over checkboxes in $_POST because checkboxes are submitted only if they are checked. If a checkbox is not checked then it is not sent to the server at all so in php you have no way of detecting its existence. In other words, you can only loop over checked checkboxes. There are two solutions how to handle this problem:
1. If the number of checkboxes and their names are predefined and constant then you need to define all of them in php and check the state of each one with isset($_POST[$checkbox_name]).
2. If you have a list of checkboxes and you don't know in advance how many of them there are you can add a hidden field in your form to accompany each checkbox so then you loop over the hidden fields, which are always submitted. The hidden fields only mark the existence of a checkbox and are identified by having the same key as the checkbox. For example:
PHP Code:
<!-- repeat this code for each location, we assume that locationsid is unique for each location: -->
<input <?php if (!(strcmp($row_rsActiveLocations['showinfooter'],"y"))) {echo "checked=\"checked\"";} ?> name="checkbox[<?php echo $row_rsActiveLocations['locationsid']; ?>]" type="checkbox">
<input type="hidden" name="checkbox_mark[<?php echo $row_rsActiveLocations['locationsid']; ?>]" value="1">
And you gather the information like this:
PHP Code:
if(isset($_POST['checkbox_mark'])){
mysql_select_db($database_db, $db);
// the value (locationsid) is passed in hidden input's key, therefore
// the value of the input itself is irrelevant ($dummy)
foreach ($_POST['checkbox_mark'] as $value => $dummy) {
$option = isset($_POST['checkbox'][$value]) ? 'y' : 'n';
$query = "UPDATE locations SET showonwebsite='$option' WHERE locationsid = $value";
$Result = mysql_query($query, $db) or die(mysql_error());
}
}
BTW, you cannot have id="checkbox[]" in multiple inputs on the same page since id must be unique.
Bookmarks