Home >Backend Development >PHP Problem >How to implement batch deletion in php ajax

How to implement batch deletion in php ajax

藏色散人
藏色散人Original
2020-08-26 09:25:062801browse

php Ajax method to implement batch deletion: first click the select all button and select all the traversed check boxes; then make ajax request to the php processing page for batch deletion; finally implement batch deletion through if statements That’s it.

How to implement batch deletion in php ajax

Recommended: "PHP Video Tutorial"

By exchanging a small amount of data with the server in the background, Ajax can make Web pages are updated asynchronously. This means updating a portion of a web page without reloading the entire page.

First click the Select All button to select all the traversed check boxes

$("#cq").click(function () {
    $(".cq").prop("checked", $(this).prop("checked"));
})

js part, using jquery

var chk = "";
var check2 = "";  //判断多个复选框中的某一个是否被全选
function checked() {
    var count = 0;
    var checkx = $("#cq");
    if (checkx.checked) {
        check2 = 1;//选中全选按钮
    } else {
        check2 = 0;//没选中全选按钮
    }
    var checkArry = $(".cq");
    for (var i = 0; i < checkArry.length; i++) {
        if (checkArry[i].checked == true) {        //选中的操作
            count++;
        }
    }
    if (count == 0) {
        chk = 0;//没有选中项
    } else {
        chk = 1;//有选中项
    }
}
function all() {
    //批量删除
    $("#plscdz").click(function () {
        checked();
        if (chk == 1 || check2 == 1) {// 提交
            $(&#39;#myModal12&#39;).modal(&#39;show&#39;);
            $("#nqrplsc").click(function () {/*给确认删除按钮加事件*/
                $(&#39;#myModal12&#39;).modal(&#39;hide&#39;);
                //找选中的主键值,用循环遍历选中的主键值
                var cq = $(".cq");
                var plstr = "";
                for (var i = 0; i < cq.length; i++) {
                    if (cq.eq(i).prop("checked")) {
                        plstr += cq.eq(i).val() + "&#39;,&#39;";
                    }
                }
                plstr = plstr.substr(0, plstr.length - 3);
                //分隔符占3个字符,截取字符串,去掉最后的"&#39;,&#39;"
                $.ajax({
                    async: false,
                    url: "aa.php",
                    data: {id: plstr},
                    dataType: "TEXT",
                    type: "POST",
                    success: function (data) {
                        if (data.trim() == "OK") {
                            alert("删除成功");
                        } else {
                            alert("删除失败");
                        }
                    }
                });
            });
        } else if (chk == 0) {
            alert("请选择您要删除的内容");
        }
    })
}

ajax to request the php processing page for batch deletion. The following is the processing page for batch deletion

session_start();
include("DBDA.class.php");
$db = new DBDA();
if (!empty($_POST["id"])) {
    $id = $_POST["id"];
    $sql = "delete from user where id in (&#39;{$id}&#39;)";
    if ($db->Query($sql, 0)) {
        echo "OK";
    } else {
        echo "NO";
    }
}

The above is the detailed content of How to implement batch deletion in php ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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