《PHP實(shí)戰(zhàn):PHP批量刪除jQuery操作》要點(diǎn):
本文介紹了PHP實(shí)戰(zhàn):PHP批量刪除jQuery操作,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP教程效果圖如下所述:
PHP教程―>―>―>
―>―>―>
PHP教程創(chuàng)建視圖show.php
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('你確定要?jiǎng)h除這'+count+'條數(shù)據(jù)嗎?')){
//獲取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>
PHP教程創(chuàng)建adminDel.php
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);
//受影響行數(shù)
echo $res;
?>
PHP教程以上所述是小編給大家介紹的PHP批量刪除jQuery操作,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/449.html