js函数:
<SCRIPT language=”javascript”>
//JavaScript Document
function allcheck(form){
var leng = form.chk.length;
if(leng == undefined){//如果变量leng没有被初始化,默认为undefined
if(!form.chk.checked){
form.chk.checked = true;
}
}else{//否则为数组时
for(i=0;i <leng;i++){
if(!form.chk[i].checked){
form.chk[i].checked = true;
}
}
}
return false;
}
function overcheck(){
var leng = form.chk.length;
if(leng == undefined){
if(!form.chk.checked){
form.chk.checked = true;
}else{
form.chk.checked = false;
}
}else{
for(i = 0;i < leng;i++){
if(!form.chk[i].checked){
form.chk[i].checked = true;
}else{
form.chk[i].checked = false;
}
}
}
return false;
}
function GetXmlHttpObject(){ //封装一个兼容浏览器的ajax函数
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
if(xmlHttp.overrideMimeType){
xmlHttp.overrideMimeType(“text/xml”);
}
}else if(window.ActiveXObject){
try{
xmlHttp=new ActiveXObject(“Msxml2.XMLHttp”);
}
catch(e){
try{
xmlHttp=new ActiveXobject(“Microsoft.XMLHttp”);
}
catch(e){
alert(“您的浏览器不支持AJAX!”);
return false;
}
}
}
return xmlHttp;
}
var xmlHttp=GetXmlHttpObject();
function del(form){
if(!window.confirm(‘是否要删除数据??’)){ //弹出一个对话框,如果是就往下执行,否则停止
}else{
var leng = form.chk.length;
if(leng == undefined){
if(!form.chk.checked){
alert(‘请选择你要删除的数据!’);
}else{
xmlHttp = GetXmlHttpObject();//调用函数GetXmlHttpObject()
var rd = form.chk.value;
var url = ‘mistake_delete.php?rd=’+rd+’&del=allDel’;
xmlHttp.open(“GET”,url,true);
xmlHttp.onreadystatechange = delnow;//处理的函数delnow
xmlHttp.send(null);
}
}else{
var leng = form.chk.length;
var rd = new Array();
var j = 0;
for( var i = 0; i < leng; i++){
if(form.chk[i].checked){
rd[j++] = form.chk[i].value;
}
}if(rd == ”){
window.alert(‘请选择你要删除的数据!’);
}else{
xmlHttp = GetXmlHttpObject();
var url = ‘mistake_delete.php?rd=’+rd+’&del=allDel’;
xmlHttp.open(“GET”,url,true);
xmlHttp.onreadystatechange = delnow;
xmlHttp.send(null);
}
}
}
return false;
}
function delnow(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var msg = xmlHttp.responseText;
if(msg != ‘1’){
window.alert(‘删除失败!Error:’+msg);
}else{
window.alert(‘删除成功’);
window.location.href=”
mistake_list.php?page=1
“;
}
}
}
}
function resultDel(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var msg = xmlHttp.responseText;
if(msg != ‘1’){
window.alert(‘删除失败!Error:’+msg);
}else{
window.location.href=”
mistake_list.php?page=1
“;
}
}
}
}
function del_single(rd){
var url = ‘mistake_delete.php?id=’+rd+’&del=oneDel’;
xmlHttp.open(“GET”,url,true);
xmlHttp.onreadystatechange = resultDel;//处理的函数delnow
xmlHttp.send(null);
window.alert(‘删除成功’);
}
</SCRIPT>
HTML页面调用:
<INPUT type=”checkbox” name=”chk[]” id=”chk” value=”{ff:$result[item].MISTAKEID/}”>
<button onClick=”return del_single({ff:$result[item].MISTAKEID/})”>单个删除</button>
<button onClick=”return allcheck(form);”>全选</button>
<button onClick=”return overcheck(form);”>反选</button>
<button onClick=”return del(form)”>删除选中</button>
PHP处理页面:
$rdd = trim($_GET[‘rd’]);
//全选删除
if(!empty($rdd) && $del = “allDel”){
$arr_id = explode(“,”,$rdd); //explode函数是把字符串分裂为一个数组
foreach($arr_id as $key=>$val){ //不确定是单选还是多选,这里使用一个foreach循环
$delete = $dbh->exec ( “DELETE FROM mistake WHERE mistakeId = ‘$val'” );
}
$reback = ‘0’;
if($delete){
$reback = ‘1’;
}else{
$reback = ‘2’;
}
echo $reback;
}
// 独立删除
if(!empty($id) && $del = “oneDel”){
$sub = $dbh->exec ( “DELETE FROM mistake WHERE mistakeId = ‘$id'” );
$reback2 = ‘0’;
if($sub){
$reback2 = ‘1’;
}else{
$reback2 = ‘2’;
}
echo $reback2;
}
转载于:https://blog.51cto.com/gesner/473959