提交 - 不工作后保持复选框数组

问题描述 投票:0回答:1

在询问我的问题之前,我已经审阅并尝试了在此类问题主题中提供的许多解决方案,因为它们都没有为我正常工作。我有一个学生的mysql表,存储了名称,标记。我正在使用while循环从数据库中检索数据。

$query="select * from student";
$rs=  mysql_query($query) or die(mysql_error());


<?php if(mysql_num_rows($rs)){ ?>
  <table border="5" cellspacing="5" width="50%" align="center">
      <tr>
          <th>No</th>
          <th>Name</th>
          <th>Marks</th>
          <th>Operation</th>
          <th>  <input type ="submit" name="delete" value="Delete"></th>
      </tr>
      <?php


 while($row=mysql_fetch_array($rs))
          {


?>
      <tr>
           <th><?php echo $row['rollno']; ?></th>
          <th><?php echo $row['name']; ?></th>
          <th><?php echo $row['marks']; ?></th>
                      <th><a href="AllOperation.php?&no=<?php echo $row['rollno']; ?>&name=<?php echo $row['name']; ?>&marks=<?php echo $row['marks']; ?>">View</a></th>
          <th><input type="checkbox" name="check[]" value="<?php echo $row['marks']; ?>" <?php if(isset($_POST['check']))  if (in_array($row['marks'], $_POST['check'])) echo "checked='checked'"; ?> /></th>
      </tr>
      <tr>
      <?php  } ?>
<input type ="submit" name="total" value="total">

而且我正在产生所有学生的总分

if(isset($_POST['total']))


{                        $t=0;
                     foreach($_REQUEST['check'] as $val)
                     {                             
                         $t=$t+$val;                             
                     }
               echo "    total : ".$t;


 }

现在的问题是,当我第一次运行程序时,它显示5个学生的信息,而不是我选择前两个复选框,然后按“总计”按钮生成总计。因此它会正确显示总数并保持选中两个复选框。但是,当我选中第三个复选框并按“总计”按钮时,它显示了三个所选复选框的标记的总数,但即使我没有选中它也会显示第四个或最后一个复选框。那么为什么会这样呢。

php html arrays checkbox checked
1个回答
3
投票

我看到的问题:if (in_array($row['marks'], $_POST['check']) echo "checked='checked'

提交时,它始终检查复选框是否具有相同的标记

我认为你应该使用另一个独特的列,ex $ row ['rollno'];而不是$ row ['marks']

© www.soinside.com 2019 - 2024. All rights reserved.