下面是我一直在运行的一个测试的代码。
<!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>
PHP代码是
<?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;
}
?>
这是为了允许我基于复选框状态构建一个1和0组成的字符串。这将用于更新具有隐藏输入值的id字段的SQL表记录。
这对于单行数据可以很好地工作,但是如果有多行数据,我就不知道如何做了。
我本以为foreach构造可能会起作用,但到目前为止还没有成功。
https://stackoverflow.com/questions/52958591
复制相似问题