Home > Backend Development > PHP Tutorial > How to implement batch deletion using php using jQuery?

How to implement batch deletion using php using jQuery?

黄舟
Release: 2023-03-14 12:16:02
Original
2060 people have browsed it

This article mainly introduces the PHP batch deletion jQuery operation. It is very good and has great reference value. Friends who need it can refer to the

The renderings are as follows:

How to implement batch deletion using php using jQuery? —>—>—>How to implement batch deletion using php using jQuery?
How to implement batch deletion using php using jQuery?—>—>—>How to implement batch deletion using php using jQuery?

Create view show.php


<?php 
 header(&#39;content-type:text/html;charset=utf-8&#39;);
 $pdo=new PDO(&#39;mysql:host=localhost;dbname=***;&#39;,&#39;root&#39;,&#39;root&#39;);
 $pdo->exec(&#39;set names utf8&#39;);
 $sql=&#39;select * from ***&#39;;
 $info=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<center>
<table border="1">
 <tr>
  <td>id</td>
  <td>title</td>
  <td>content</td>
 </tr>
 <?php foreach($info as $k => $v){ ?>
 <tr>
  <td><input type="checkbox" name="box" value="<?= $v[&#39;id&#39;] ?>"><?= $v[&#39;id&#39;] ?></td>
  <td><?= $v[&#39;title&#39;] ?></td>
  <td><?= $v[&#39;content&#39;] ?></td>
 </tr>
 <?php } ?>
</table>
 <button>批量删除</button>
</center>
<script src="../jquery.1.12.min.js"></script>
<script>
 $(function(){
  $(&#39;button&#39;).click(function(){
   var ids=$(&#39;:checkbox&#39;);
   var str=&#39;&#39;;
   var count=0;
   for(var i=0;i<ids.length;i++){
    if(ids.eq(i).is(&#39;:checked&#39;)){
     str+=&#39;,&#39;+ids.eq(i).val();
     count++;
    }
   }
   var str=str.substr(1);
   if(confirm(&#39;你确定要删除这&#39;+count+&#39;条数据吗?&#39;)){
    //获取id后删除
    $.ajax({
     type:&#39;get&#39;,
     url:&#39;adminDel.php&#39;,
     data:{str:str},
     success:function(res){
      if(res>0){
       alert(&#39;删除成功&#39;);
       for(var i=ids.length-1;i>=0;i--){
        if(ids.eq(i).is(&#39;:checked&#39;)){
         ids.eq(i).parent().parent().remove();
        }
       }
      }
     }
    })
   }
   return false;
   /*var box=document.getElementsByName(&#39;box&#39;);
   var str="";
   for(var i=0;i<box.length;i++){
    if(box[i].checked==true){
     str+=&#39;,&#39;+box[i].value;
    }
   }
   var str=str.substr(1);
   alert(str);*/
  });
 })
</script>
Copy after login

Create adminDel.php


<?php 
 header(&#39;content-type:text/html;charset=utf-8&#39;);
 $str=$_GET[&#39;str&#39;];
 $pdo=new PDO(&#39;mysql:host=localhost;dbname=***;&#39;,&#39;root&#39;,&#39;root&#39;);
 $pdo->exec(&#39;set names utf8&#39;);
 $sql=&#39;delete from *** where id in (&#39;.$str.&#39;)&#39;;
 $res=$pdo->exec($sql);
 //受影响行数
 echo $res;
?>
Copy after login

The above is the detailed content of How to implement batch deletion using php using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template