<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<input type="checkbox" id="a" value="zs" />张三<br />
<input type="checkbox" id="b" value="ls" />李四<br />
<input type="checkbox" id="c" value="ww"/>王五<br />
<input type="button" value="批量删除" />
</body>
</html>以上是一个php文件,请问,如何实现批量删除。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<body>
<input type="checkbox" id="a" value="zs" />张三<br />
<input type="checkbox" id="b" value="ls" />李四<br />
<input type="checkbox" id="c" value="ww"/>王五<br />
<input type="button" value="批量删除" />
</body>
</html>以上是一个php文件,请问,如何实现批量删除。
function clickch(request)
{
var CheckedRecs = document.getElementById("CheckedRecs");
var chk=request;
if(chk.checked == true)
CheckedRecs.value += chk.id + ",";
else
{
var start = CheckedRecs.value.indexOf(chk.id + ",");
var end = start + chk.id.length + 1;
CheckedRecs.value = CheckedRecs.value.substring(0,start) + CheckedRecs.value.substring(end,CheckedRecs.value.length);
}
if(CheckedRecs.value == '')
{
document.getElementById("Delete").disabled = true;
}
else
{
document.getElementById("Delete").disabled = false;
}
//alert(CheckedRecs.value);
}</script>
<input type="submit" name="Delete" id="Delete" value="删除" disabled="true"/>
<INPUT type="hidden" id="CheckedRecs" name="CheckedRecs" value="">
<INPUT type="checkbox" id="1" name="checkboxes" size="10" onclick="clickch(this)">张三
<INPUT type="checkbox" id="2" name="checkboxes" size="10" onclick="clickch(this)">李四
<INPUT type="checkbox" id="3" name="checkboxes" size="10" onclick="clickch(this)">王五
<INPUT type="checkbox" id="4" name="checkboxes" size="10" onclick="clickch(this)">赵八按照上面的在你点删除时,在php里面获取CheckedRecs的值,就能取到每个人物对应的id了.
全选的按钮的事件你自己搞吧.
var flag=0;
function checkall(){
for(var i = 0; i < document.form1.elements.length; i++) {
var e = document.form1.elements[i];
if(e.type=="checkbox"){
if(flag==0){
e.checked=true;
}
else{
e.checked=false;
}
}
}
if(flag==0)
flag=1;
else flag=0;
}
</script>
<form name="form1" method="post" action="del.php">
<input type="checkbox" id="id[]" value="zs" />张三<br />
<input type="checkbox" id="id[]" value="ls" />李四<br />
<input type="checkbox" id="id[]" value="ww"/>王五<br />
<input type="checkbox" onclick="checkall()">全选/不选
<input type="submit" value="批量删除" />
</form>del.php:
isset($_POST['id']) ? $id = $_POST['id']:$nid = array();
if(count($id)==0){
echo "<script>alert(\"出错:没有选择删除项!\");window.location.href='".$referer."';</script>";
exit();
}elseif(count($id) > 0){
foreach($id as $n){
@mysql_query( "delete from `table` where `id`='{$n}'" );
}
echo "<script>alert(\"删除成功!\");window.location.href='".$referer."';</script>";
}
更正一下:
isset($_POST['id']) ? $id = $_POST['id']:$id = array();
1.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php批量删除</title>
</head>
<script src="jquery-1.4.2.min.js"></script>
<script>
$(function(){
$('#checkAll').click(function(){
$("input[name='userName[]']").each(function(){
if($(this).attr("checked")){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
});
})
</script>
<body>
<form id="del" name="del" action="">
<input type="checkbox" name="checkAll" id="checkAll" />选择<br />
<input name="userName[]" type="checkbox" id="a" value="1" />张三<br />
<input name="userName[]" type="checkbox" id="b" value="2" />李四<br />
<input name="userName[]" type="checkbox" id="c" value="3"/>王五<br />
<input type="button" name="del" value="批量删除" />
</form>
</body>
</html>
<?php
if(isset($_POST['del']) && trim($_POST['del']) == '批量删除')
{
$arrId = $_POST['userName'];
$userId = implode(',',$arrId);
mysql_query('DELETE FROM `user` WHERE `userId` IN ($userId)');
}
?>
$userId = implode(',',$arrId);
mysql_query('DELETE FROM `user` WHERE `userId` IN ($userId)');
-------------------------------
这个方法好 效率高 推荐
<?php
echo '<pre>';
echo var_export($_POST['ids']);
echo '</pre>';
?>
<form method="post">
<input type="checkbox" name="ids[]" value="3">张三
<input type="checkbox" name="ids[]" value="4">李四
<input type="checkbox" name="ids[]" value="5">王五
<input type="submit">
</form>以上代码在点击“提交”后得到的是:
array (
0 => '3',
1 => '4',
2 => '5',
)
后面怎么在数据库中删除就不用说了吧?