正如 Dharman 所建议的,在您的 while 循环中使用迭代器来创建您的 name 属性,以便每个名称都不同。然后,一旦您的用户点击提交按钮,全局 $_POST 变量将保存他们选择的复选框的值。
考虑以下代码示例..
// define a variable to hold your output in...
$output = null;
// as I do not have your database result, I will use an example array of values
// to get a length to use in a while loop to show the iteration using a declared number
// that is iterated within your while loop until the length of the array is reached
$array = [1,2,3,4,5,6,7,8]; //--> Here we have 8 values in the array
// define a number to use as iterator or a counter
$i = 1;
// while loop iterates through array while our counter is less than the number within our array
while($i < count($array)){
// concatenate our output variable to include the next input
// Each time we iterate through to the next value in our array
// we concatenate the next numbers iterated value by 1 to our name attribute
// now your name attribute is uniquely named
$output .= '<input type="checkbox" name="selectSystem'.$i.'"/>';
$i++;
}
输出:
<input type="checkbox" name="selectSystem1"/>
<input type="checkbox" name="selectSystem2"/>
<input type="checkbox" name="selectSystem3"/>
<input type="checkbox" name="selectSystem4"/>
<input type="checkbox" name="selectSystem5"/>
<input type="checkbox" name="selectSystem6"/>
<input type="checkbox" name="selectSystem7"/>
<input type="checkbox" name="selectSystem8"/>
现在,一旦您想在用户按下表单上的提交按钮后访问这些值,该数组就存在于您的 $_POST 数组中。只需检查 'on' 的严格条件即可访问已选中复选框的值...
// we define a variable to hold our results and assign a null value to it so when not set it shows nothing...
$stmt = null;
// if our $_POST isset...
if(isset($_POST)){
// loop through results and use a strict conditional on the $value each loop through
foreach($_POST as $key => $value){
if($value === 'on'){
// concatenate the variable to hold the key and value foreach result
$stmt .= $key.' -> '.$value.'<br>';
}
};
}
<form method="post">
<?=$output?><!--/ Echo your $output which holds the inputs/-->
<input type="submit" value="submit" name="submit">
</form>
<?=$stmt?><!--/ Echo your $stmt which holds the $_POST array values of your checkboxes that are 'on', if this variable is null it shows nothing in your html /-->