PHP -复选框表单不会返回自己的点击


PHP - checkbox form wont return itself as clicked

我有一个名为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
...

我在你的代码片段中没有看到

标签。确保你有method="post",否则它将默认为get请求。您也可以从$_POST切换到$_GET或$_REQUEST。如果不是这样,发布更多的代码会有所帮助。