全选,反选,批量删除

<?php
header("content-type:text/html;charset=utf-8");
//连接数据库
	$link=mysql_connect('127.0.0.1','root','root')or dae("连接失败");
//选择数据库
	mysql_select_db('kaoshi',$link);
//设置字符集
	mysql_query("set names utf8");
//写sql语句
	$sql="select * from denglu";
//执行sql语句
	$res=mysql_query($sql);
?>
<table border=1>
	<tr>
      <td><input type="checkbox" name="quan" οnclick="check_quan()">全选</td>
      <td>序号</td>
      <td>姓名</td>
      <td>密码</td>
      <td>操作</td>
	</tr>
	<?php
	while($arr=mysql_fetch_assoc($res)){


	?>
	<tr>
      <td><input type="checkbox" name="box" value="<?php echo $arr['id'];?>"/></td>
      <td><?php echo $arr['id'];?></td>
      <td><?php echo $arr['c_name'];?></td>
      <td><?php echo $arr['c_pwd'];?></td>
      <td><a href="javascript:del(<?php echo $arr['id'];?>)">删除</a></td>
	  
     </tr>
<?php

}	
?>
<tr>
<td colspan="5"><button οnclick="Quan()">全选</button> 
	      <button οnclick="Fan()">反选</button> 
	      <button οnclick="Bu()">全不选</button>
	      <button οnclick="Pi()">批量删除</button>
</td>
</tr>
</table>
<script type="text/javascript">
//默认选中
function check_quan(){
      var quan =document.getElementsByName('quan')[0]
	  var box =document.getElementsByName('box')
		  for(var i=0;i<box.length;i++){
		   if(quan.checked==true){
                     box[i].checked=true;
		   }else{
               box[i].checked=false;
		   }
}
}
//全选
function Quan(){
     var box =document.getElementsByName('box')
		  for(var i=0;i<box.length;i++){
		      box[i].checked=true;
		   
		   }
}
//全不选
 function Bu(){
     var box =document.getElementsByName('box')
		  for(var i=0;i<box.length;i++){
		      box[i].checked=false;
		   
		   }
}
//反选
 function Fan(){
     var box =document.getElementsByName('box')
		  for(var i=0;i<box.length;i++){
		      box[i].checked=!box[i].checked;
		   
		   }
}
//单条删除
function del(obj){
     if(confirm('是否删除')){    
           location.href='del.php?id='+obj;
	 }
}
//批量删除
 function Pi(){
	var str="";
      
	  var box =document.getElementsByName('box');
          
		  for(var i=0;i<box.length;i++){
		   if(box[i].checked==true){
                    str =str+','+box[i].value;
 
		   }
}
      str=str.substr(1);
	  alert(str);
    if(confirm('是否批量删除')){    
           location.href='del.php?id='+str;
	 }
}

</script>




//删除页面
<pre name="code" class="html"><?php
header("content-type:text/html;charset=utf-8");
$id=$_GET['id'];
//连接数据库
	$link=mysql_connect('127.0.0.1','root','root')or dae("连接失败");
//选择数据库
	mysql_select_db('kaoshi',$link);
//设置字符集
	mysql_query("set names utf8");
//写sql语句
	$sql="delete from denglu where id in($id)";
//执行sql语句
	$res=mysql_query($sql);
if($res){
       echo "<script>alert('删除成功');location.href='rikao.php'</script>";
}else{ echo "<script>alert('删除失败');location.href='rikao.php'</script>";}
?>

 
 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值