Home  >  Article  >  Backend Development  >  How to implement batch deletion function with PHP jQuery+Ajax

How to implement batch deletion function with PHP jQuery+Ajax

墨辰丷
墨辰丷Original
2018-05-22 14:49:581252browse

This article mainly introduces the method of PHP jQuery AjaxPHP jQuery Ajax to implement the batch deletion function. Friends who need it can refer to it

For the sake of beauty, I still introduced the bootstrap modal box. What I introduced is A table in my own database library is named: maninfo table is a personal information table

I will not write the loading of the table, it is relatively simple. Just write the required buttons and html part.

<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, click the Select All button to 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

<p class="modal fade" id="myModal11" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <p class="modal-dialog">
            <p class="modal-content">
              <p class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                  ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                  提示
                </h4>
              </p>
              <p id="qrnr1" class="modal-body">
                您将删除选中的图书!
              </p>
              <p 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>
              </p>
            </p><!-- /.modal-content -->
          </p><!-- /.modal -->
        </p>
      </p>

This way the front-end content is completed, and then I started writing the 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;//有选中项
    }
    //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 connect 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";
  }
}

Written here, 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 delete the batch directly. Just write the method and call it in the loading method.

Related recommendations:

How to implement batch deletion in php

ThinkPHP implementationBatch deletioncolumn method

##PHP implementationBatch deletionjQuery operation method

The above is the detailed content of How to implement batch deletion function with PHP jQuery+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