2015-06-17 39 views
2

我有以下格式的复选框。当提交表格时,请保留复选框

<?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状态?

回答

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> 
0

你可以尝试使用这个code.Hope这将工作(也可尝试使用库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>"; 
      ?>