donglu8779 2014-07-22 04:24
浏览 15
已采纳

如何使用php中的复选框删除记录?

Need to delete records and Here is my code:

<input type="submit" name="delete" value="delete"/>  
<?php
    if(isset($_POST['delete'])) 
    {  
        $cnt=array(); 
        $cnt=count($_POST['chkbox']);  
        for($i=0;$i<$cnt;$i++)   
        {
            $del_id=$_POST['chkbox'][$i];
            $query="DELETE FROM menu_detail WHERE vt_id=".$del_id;
            mysql_query($query);   
        } 
    } 
?>  
// I draw checkboxes using this where as I m fetching data from table...
<?php  
   $col = "row0"; $i = 0; $a=1;
   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
   if ($i == 0)  { $col = "row0"; $i = 1; } else { $col = ""; $i = 0; }
?>
<tr class="<?php echo $col; ?>">
<td>
    <input type="checkbox" name="chkbox" value="checkbox[<?php echo $row['vt_id']?>]" />
</td>

Look at the image will give you an idea where I am stuck

enter image description here

  • 写回答

1条回答 默认 最新

  • drhzc64482 2014-07-22 04:42
    关注

    Take a look

           $tableRow = array("id","name","code"): // assume It must from Database
    
             <form action="your_action.php"  method="post" id="bulkDel" >
    
           <table>
                   <thead>
                          <th><input type="checkbox" id="checkAll" /></th>
                          <th>....</th> <!--and so on-->
                  </thead>
               <?php
                     foreach($tableRow as $row){
                           ?>
                         <tr>
                               <td> <input tyep="checkbox"  name="check[]" /></td>
                               <td>.....</td> <!-- and so on --> 
                         </tr>
    
                      <?php
    
                     }
                ?>
    
          </table>
          <a href="javascript:void(0);"id="bulkDeleteBut" >Delete selected</a>
       </form>
    
      // jquery check all checkboxes
    
      $(document).on('click','#checkAll',function(){
                        $('input[name="check[]"]').prop('checked',$(this).is(":checked"));
      });
    
    
     $(document).on('click','#bulkDeleteBut',function(){
                 var len = $('input[name="check[]"]')..serializeArray().length;
                 if(len>0){
                         $('#bulkDel').submit();
                 }else{
                  alert("Check atleast one record");
                } 
     });
    
    
     // server side php
       if(isset($_POST['check'])){
           $ar = $_POST['check'];
            foreach($ar as $primary_key){
                 // perform mysql delete query and return back to table page
            }
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计