<?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>";}
?>
全选,反选,批量删除
最新推荐文章于 2021-12-02 14:44:34 发布