Home >Backend Development >PHP Tutorial >How to implement batch deletion of jQuery operations in PHP
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:
—>—>—>
—>—>—>
Create view show.php
<?php header('content-type:text/html;charset=utf-8'); $pdo=new PDO('mysql:host=localhost;dbname=***;','root','root'); $pdo->exec('set names utf8'); $sql='select * from ***'; $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['id'] ?>"><?= $v['id'] ?></td> <td><?= $v['title'] ?></td> <td><?= $v['content'] ?></td> </tr> <?php } ?> </table> <button>批量删除</button> </center> <script src="../jquery.1.12.min.js"></script> <script> $(function(){ $('button').click(function(){ var ids=$(':checkbox'); var str=''; var count=0; for(var i=0;i<ids.length;i++){ if(ids.eq(i).is(':checked')){ str+=','+ids.eq(i).val(); count++; } } var str=str.substr(1); if(confirm('你确定要删除这'+count+'条数据吗?')){ //获取id后删除 $.ajax({ type:'get', url:'adminDel.php', data:{str:str}, success:function(res){ if(res>0){ alert('删除成功'); for(var i=ids.length-1;i>=0;i--){ if(ids.eq(i).is(':checked')){ ids.eq(i).parent().parent().remove(); } } } } }) } return false; /*var box=document.getElementsByName('box'); var str=""; for(var i=0;i<box.length;i++){ if(box[i].checked==true){ str+=','+box[i].value; } } var str=str.substr(1); alert(str);*/ }); }) </script>
Create adminDel.php
<?php header('content-type:text/html;charset=utf-8'); $str=$_GET['str']; $pdo=new PDO('mysql:host=localhost;dbname=***;','root','root'); $pdo->exec('set names utf8'); $sql='delete from *** where id in ('.$str.')'; $res=$pdo->exec($sql); //受影响行数 echo $res; ?>
Related recommendations:
php Delete Methods for specifying folders
PHP recursively implements folder copying, delete, viewing size, etc.
PHP implements folder copy, delete, view size, etc. based on iteration
##
The above is the detailed content of How to implement batch deletion of jQuery operations in PHP. For more information, please follow other related articles on the PHP Chinese website!