【问题标题】:Keep checkboxes checked when the form is submitted提交表单时保持复选框处于选中状态
【发布时间】:2015-06-17 05:28:37
【问题描述】:

我有以下带有复选框的表单。

<?php
   include 'connection.php';
   $sql="SELECT * FROM `tbl`";
   $query=mysqli_query($connect,$sql);
   echo "<table border='2px'>
             <thead>
                 <th>ID</th>
                 <th>Title</th>
                 <th>Name</th>
                 <th>Doing</th>
                 <th>Done</th>
             </thead>
             <tbody>
                 <form method='POST'>";
                      while($res=mysqli_fetch_assoc($query)){
                          $id=$res['id'];
                          echo"<tr>
                                  <td>{$res['id']}</td>
                                  <td>{$res['title']}</td>
                                  <td>{$res['name']}</td>
                                  <td><input type='checkbox' name='doing[]' value='".$res['id'].$res['title'].$res['name']."'></td>
                                  <td><input type='checkbox' name='done[]' value='".$res['id'].$res['title'].$res['name']."'></td>
                               </tr>";
                      }
                      echo "<input type='submit' value='OK' name='btn'>
                 </form>
             </tbody>
         </table>";
?>

如何让复选框在提交表单时保持checked 状态?

【问题讨论】:

    标签: javascript php jquery ajax checkbox


    【解决方案1】:

    我假设您不希望同时选中 doingdone,因此我将复选框替换为单选按钮,因为这是它们的预期行为。

    我更改了单选按钮的名称和值,以便可以在 PHP 中轻松访问它们,并且从您提供的代码来看,它看起来并不像您之后使用的名称和值一样。

    我清理了标记和脚本布局,使其更具可读性。

    如果我偏离您的原始代码的意图太远,请告诉我。

    注意:这将显示发布的值,但不会保存这些值到数据库中。

    (Demo)

    <?php
        include('connection.php');
        $sql = "SELECT * FROM `tbl`";
        $query = mysqli_query($connect,$sql);
    
        function get_checked ( $id ) {
            if ( isset($_POST) && isset($_POST['checked'][$id]) ) {
                return $_POST['checked'][$id];
            }
            return false;
        }
    ?>
    <form method="post">
        <table border="2px">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Name</th>
                    <th>Doing</th>
                    <th>Done</th>
                </tr>
            </thead>
            <tbody>
                <?php while ( $res = mysqli_fetch_assoc ( $query ) ): ?>
                    <?php $checked = get_checked ( $res['id'] ) ?>
                    <tr>
                        <td><?= $res['id'] ?></td>
                        <td><?= $res['title'] ?></td>
                        <td><?= $res['name'] ?></td>
                        <td><input type="radio" name="checked[<?= $res['id'] ?>]" value="doing" <?= $checked === "doing" ? 'checked' : '' ?>></td>
                        <td><input type="radio" name="checked[<?= $res['id'] ?>]" value="done" <?= $checked === "done" ? 'checked' : '' ?>></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
        <input type='submit' value='OK' name='btn'>
    </form>
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用此代码。希望这会起作用(也尝试使用 mysqli 或 PDO):

      <?php
               include 'connection.php';
               $sql="SELECT * FROM `tbl`";
               $query=mysqli_query($connect,$sql);
      
                  echo "<table border='2px'>
            <thead>
            <th>ID</th>
            <th>Title</th>
            <th>Name</th>
            <th>Doing</th>
            <th>Done</th>
             </thead>";
               echo "<form method='POST'>";
              echo "<tr>";
              $s=0;
              while($res=mysqli_fetch_assoc($query)){
              $id=$res['id'];
              $doing_v="";
              $done_v="";
              $c_doing="";
              $c_done="";
      
              if(isset($_POST['doing'][$s])){
              $doing_v=$_POST['doing'][$s];
              }
      
              if(isset($_POST['done'][$s])){
              $done_v=$_POST['done'][$s];
              }
      
              if($doing_v=='".$res['id'].$res['title'].$res['name']."'){
              $c_doing="checked";
              }
      
              if($done_v=='".$res['id'].$res['title'].$res['name']."'){
              $c_done="checked";
              }
      
                    echo" <td>{$res['id']}</td>
                     <td>{$res['title']}</td>
                     <td>{$res['name']}</td>
              <td><input type='checkbox' name='doing[]'
              value='".$res['id'].$res['title'].$res['name']."' ".$c_doing." ></td>
      
                     <td><input type='checkbox' name='done[]'  
               value='".$res['id'].$res['title'].$res['name']."' ".$c_done." ></td>
                     </tr>";
                     $s++;
                     }
      
                     echo "</table>";
                     echo "<input type='submit' value='OK' name='btn'>";
                     echo "</form>";
                ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多