Below is the code for a test I have been running.
<!DOCTYPE HTML">
<html>
<head>
<title>Test Page</title>
</head>
<body>
<form class="form-horizontal" method = "POST">
<fieldset>
<table class = "table table-striped table-sm">
<thead>
<tr>
<th style="width: 2%"> </th>
<th style="width: 18%">Name</th>
<th style="width: 6%"> W1 </th>
<th style="width: 6%"> W1 </th>
<th style="width: 6%"> W1 </th>
<th style="width: 6%"> W1 </th>
<th style="width: 6%"> W1 </th>
<th style="width: 6%"> W1 </th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="memberid[]" value="1063" /></td>
<td>Charlie Farnsworth</td>
<td align="center"><input type="checkbox" name="test[]" value = "0" ></td>
<td align="center"><input type="checkbox" name="test[]" value = "1" ></td>
<td align="center"><input type="checkbox" name="test[]" value = "2" ></td>
<td align="center"><input type="checkbox" name="test[]" value = "3" ></td>
<td align="center"><input type="checkbox" name="test[]" value = "4" ></td>
<td align="center"><input type="checkbox" name="test[]" value = "5" ></td>
</tr>
</tbody>
</table>
</fieldset>
<div align="center">
<br /><br />
<input type="submit" id= "submit" name = "submit" value="Save changes">
<br /><br />
</div>
</form>
</body>
</html>
The PHP code is
<?php
if (isset($_POST)) {
$return = '000000';
if (!empty($_POST['test'])) {
for ($x = 0; $x < 6; $x++) {
if(IsChecked('test',$x)){
$return=substr_replace($return,"1",$x,1); }
}
}
$return = implode(',',str_split($return));
echo $_POST['memberid'].'<br/>';
echo $return.'<br/><br/>';
}
unset($_POST);
}
function IsChecked($chkname,$value) {
if(!empty($_POST[$chkname])){
foreach($_POST[$chkname] as $chkval){
if($chkval == $value){
return true;
}
}
}
return false;
}
?>
This is to allow me to build a string of 1s and 0s based on the checkbox states . This will be used to update a SQL table record having an id field of the value of the hidden input.
This works fine for a single row of data, but I am at a loss of how to do it if there are several rows of data.
I would have thought a foreach construct might work, but so far have been unsuccessful.