How to use php+ajax to select and delete all

墨辰丷
Release: 2023-03-28 11:56:01
Original
1550 people have browsed it

This article mainly introduces the method of php ajax to realize all selection and deletion. It analyzes the specific operation steps and related techniques of html js front-end selection and batch deletion through ajax and background php interaction in the form of examples. Friends who need it can Refer to the following

for details:

<input type="checkbox" id="ckb_selectAll" onclick="selectAll()" title="选中/取消选中">
<a href="javascript:void(0);" onclick="del_()" title="删除选定数据" style="font-weight:normal">删除</a>
Copy after login

↑Select all checkbox

<input type="checkbox" class="ckb" id="+con.id+" value="+con.id+">
Copy after login

↑ is the deleted item, the same named class is ckb, which is convenient for operation. At the same time, the id value is cleverly put into the input for easy access.

function selectAll() {
  if ($(&#39;#ckb_selectAll&#39;).is(&#39;:checked&#39;)) {
    $(".ckb").attr("checked", true); //全部选中
  } else {
    $(".ckb").attr("checked", false);//全部取消
  }
}
Copy after login

↑Selected event

function del_() {
  var ids = &#39;&#39;;
  $(".ckb").each(function() {
    if ($(this).is(&#39;:checked&#39;)) {
      ids += &#39;,&#39; + $(this).val(); //逐个获取id
    }
  });
  ids = ids.substring(1); // 对id进行处理,去除第一个逗号
  if (ids.length == 0) {
    alert(&#39;请选择要删除的选项&#39;);
  } else {
    if (confirm("确定删除?删除后将无法恢复。")) {
      url = "action=del_call_record&ids=" + ids;
      $.ajax({
        type: "post",
        url: "send.php",
        data: url,
        success: function(json) {
          if (parseInt(json.counts) > 0) {
            alert(json.des);
            location.reload();
          } else {
            alert(json.des);
          }
        },
        error: function(XMLHttpRequest, textStatus) {
          alert("页面请求错误,请检查重试或联系管理员!\n" + textStatus);
        }
      });
    }
  }
}
Copy after login

↑Deletion is handled with ajax.

↓Operate the database in the background and process deletion actions.

$ids = trim($_REQUEST[&#39;ids&#39;]);
$del_sql = "DELETE FROM vicidial_call_record WHERE id IN(".$ids.")";
//print_r($del_sql);exit;
if (mysqli_query($db_conn, $del_sql)) {
  $counts = "1";
  $des = "成功";
} else {
  $counts = "0";
  $des = "失败";
}
$json_data = "{";
$json_data. = "\"counts\":".json_encode($counts).",";
$json_data. = "\"des\":".json_encode($des)."";
$json_data. = "}";
echo $json_data;
break;
Copy after login

Complete

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

References and returns of php functions

ThinkPHPHow to use Memcached to cache data in the framework

##PHPVerification code class ValidateCode

The above is the detailed content of How to use php+ajax to select and delete all. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!