Home  >  Article  >  Backend Development  >  How to use php ajax to implement batch deletion function

How to use php ajax to implement batch deletion function

藏色散人
藏色散人Original
2020-08-27 09:12:212412browse

php Ajax method to implement batch deletion: first introduce the bootstrap modal box and database table; then create the html part of the "batch delete" button; and finally connect ajax to the PHP processing page for batch deletion.

How to use php ajax to implement batch deletion function

Recommended: "PHP Video Tutorial"

PHP jQuery Ajax combined with write batch deletion function

For the sake of beauty, I still introduced the bootstrap modal box. What I introduced was a table in my own database library named: The maninfo table is a personal information table. I will not load the table. Written, it is relatively simple, just write the required buttons and html parts

<button type="button" class="btn btn-primary"  id="plscdz" >批量删除</button>

Select all:

<input type="checkbox" id="cq"/>

The traversed check boxes are

<input type="checkbox" value="{$v[0]}" class="cq"/>

First After clicking the Select All button, you can select all the traversed check boxes

<script type="text/javascript">
 $("#cq").click(function(){
$(".cq").prop("checked",$(this).prop("checked"));
})
</script>

Here I only wrote a simple modal box

<div class="modal fade" id="myModal11" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                  ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                  提示
                </h4>
              </div>
              <div id="qrnr1" class="modal-body">
                您将删除选中的图书!
              </div>
              <div class="modal-footer">
                <button id="qxplsc" type="button" class="btn btn-default" data-dismiss="modal">取消删除</button>
                <button id="qrplsc" type="button" class="btn btn-primary">确认批量删除</button>
              </div>
            </div><!-- /.modal-content -->
          </div><!-- /.modal -->
        </div>
      </div>

In this way, the content of the front end is completed, so I started writing the js part. I used all 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;//有选中项
    }
    //alert(chk);
  }
  function plscdzxx()
  {
    //批量删除
    $("#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;",这样正好匹配SQL语句
          $.ajax({
            async:false,
            url:"plscdz.php",
            data:{plstr:plstr},
            dataType:"TEXT",
            type:"POST",
            success:function(data){
              if(data.trim()=="OK")
              {
                alert("删除成功");
                nload();  //在这里要重新加载一遍页面
              }
              else
              {
                alert("删除失败");
              }
            }
          });
        });
      }
      else if(chk==0)
      {
        // 不提交
        //alert(chk);
        alert("请选择您要删除的内容");
      }
    })
  }

ajax will be connected to the batch deletion processing page. The following is the batch deletion processing page

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

Write it here if you want If you try it yourself, it may not work. In this case, you need to adjust the batch deletion method. If there is a loading method before, then just write the batch deletion method directly into the loading method and call it.

The above is the detailed content of How to use php ajax to implement batch deletion function. 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