我正在尝试创建一个学生出勤表,其中包含每个学生出勤的复选框,该复选框将存储在数据库中。
这是我迄今为止拥有的表,它是通过 ajax 函数调用执行的。大多数数据来自包含学生姓名的数据库。
就本示例而言$numStudent = 5;
echo '<form method="post" action="checkAttend.php">
<table border="1">';
$count = 1;
while(($count < $numStudent) && ($students = mysqli_fetch_assoc($result))) {
echo '<tr>
<th>' . $count. '</th>
<th>' . $students['student_name'] . '</th>
<th> <input type="checkbox" name="students[]" /> </th>
</tr>';
$count++;
}
echo '<th colspan = "3">
<input type="submit" name="button" id="button" value="Submit" /> </th>';
echo '</table>';
echo '</form>';
在 checkAttend.php
中,表单完全按照我需要的方式打印,但是当我尝试打印复选框数组中的值时(在将其保存到之前检查其中包含哪些值)数据库)打印出数组中的每个值:
$student_attend[0] A
$student_attend[1] r
$student_attend[2] r
$student_attend[3] a
$student_attend[4] y
通常我想在数据库的相应字段中存储某种值来指示学生是否缺勤。
我无法判断我是否正确执行了复选框循环,或者是否需要添加更多内容。
最佳答案
您需要循环遍历 $student_attend
数组,如下所示:
foreach ($student_attend as $value) {
// do something with $value
}
有关 foreach
的更多信息 here .
关于php - php 创建多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38195196/