我有一个名为present的变量,我也有一个复选框,我需要这样做,如果复选框被选中,然后有人提交表单,$present
变量将更改为1的值。现在什么都不会发生如果框被选中,如果我从
if(isset($_POST['stud_attendance']))
if(!isset($_POST['stud_attendance']))
则它将工作,因为else将$present
变量设置为1,但由于某些原因,代码没有意识到复选框已被选中。
$present = 1;
while($row = mysqli_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>";
echo $row[6];
?>
//below is the code****************************************************
<input type="checkbox" name="stud_attendance" value="0">
<?php
if(isset($_POST['stud_attendance']))
{
$present = 1;
}
else
{
$present = 0;
}
// above is the code ******************************
echo $present;
// above is just to check the value of the variable
echo "</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[1]."</td>";
echo "</tr>";
}
echo "</table>";
你可以使用javascript,像这样在提交输入:
onclick="if(this.value=='0') this.value='1'"
首先,您可以检查post数组中的内容:
print_r($_POST);
如果复选框被选中,它应该显示为:
...
[stud_attendance] => on
...
我在你的代码片段中没有看到