我想创建一个考勤记录表单,教师可以记录学生的考勤并将其发送到数据库中存储。在这里,我使用单选按钮,我希望这些按钮只选择存在或不存在或授权,但现在它没有实现我想做的事情。现在,单选按钮的选择不能正常工作,所以你能请我把单选按钮在特定的方式,使它只选择存在或缺席或授权。我认为我们必须使用CSS或javaScript的HTML表单语法,但问题是如何。提前谢谢你。
echo '<table action="process.php" method="POST" class="tableEchoPupilAttendance" border="1">
<tr>
<th>Image</th>
<th>Name</th>
<th>Present</th>
<th>Absent</th>
<th>Authorise</th>
</tr>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>
<td><img width='70' height='60' src='data:image/jpeg;base64,".base64_encode($row['image'])."'/></td>
<td>" .$row['name']. $row['surname']."</td>
<td> <input type='radio' name='present' value=''/> </td>
<td> <input type='radio' name='absent' value=''/> </td>
<td> <input type='radio' name='late' value=''/> </td>
</tr>";
}
echo "</table>";
单选按钮按name
属性分组。要使每行上的三个单选按钮中的一个可选,它们的name
必须相同,并且它们所需的值存储在value
属性中。
我认为这是公平的假设你有某种id
对你的结果行。因此下面的代码可以工作:
while ($row = mysqli_fetch_array($result)) {
?>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="present" /><td>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="absent" /><td>
<td><input type="radio" name="att[<?=$row['id'];?>]" value="late" /><td>
<?
}
提交这将给你一个数组的内容,如:
[1] => 'present',
[3] => 'absent',
[4] => 'present',
[6] => 'late'
使用数值通常是一个好主意,但是代码将不那么可读。
单选按钮只有在具有相同名称时才会被分组。所以你要做的是:
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='present'/> </td>
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='absent'/> </td>
<td> <input type='radio' name='attendance[<?php print $row['id']; ?>]' value='late'/> </td>