怎么在PHP中使用ajax和jQuery 实现一个批量删除功能

发布时间:2021-05-12 17:32:38 作者:Leah
来源:亿速云 阅读:102

怎么在PHP中使用ajax和jQuery 实现一个批量删除功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

目录结构

怎么在PHP中使用ajax和jQuery 实现一个批量删除功能

piliangshan.php

<?php 
  require_once './db_conn.php';
  $sql = "select * from user";
  $result = mysqli_query($conn, $sql);
?>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>全选演示</title>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="stylesheet" type="text/css" href="./static/bootstrap.min.css" rel="external nofollow" >
  <script src="./static/jquery.js"></script>
  <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0">
</head>
<body>
  <form enctype="multipart/form-data" method="post">
    <div class="bs-example" data-example-id="simple-table" >
      <table class="table" id="J-dl">
        <a href="javascript:void(0);" rel="external nofollow" class="btn btn-danger" onclick="selectAll()" title="删除选定数据" >批量删除</a>
        <thead>
          <tr>
            <th><input type="checkbox" id="J-all" class="ckb"></th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Username</th>
          </tr>
        </thead>
        <tbody>
          <?php 
          while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            echo  '<tr>
            <th><input type="checkbox" class="ck" id="ck-1" value="'.$row['id'].'"></th>
            <th scope="row">'.$row['id'].'</th>
            <td>'.$row['username'].'</td>
            <td>'.$row['sort'].'</td>
            </tr>';
          }
          ?>
        </tbody>
      </table>
    </div>  
  </form>
  <script>
    (function () {
      var $all = $('#J-all');
      var $dl = $('#J-dl');

      // 绑定全选按钮点击事件,让下面所有的复选框是跟全选的一样
      $all.on('click', function () {
        $dl.find('.ck').prop('checked', !!this.checked);
      });

      // 绑定点击所有的复选框,点击的时候判断是否页面中全选了
      $dl.find('.ck').on('click', function () {
        // 我只是喜欢用filter(fn),用选择器也行
        // 查找没有选择的元素
        var $unSelectedElem = $dl.find('.ck').filter(function () {
          return !this.checked;
        });

        // 如果有没有选中的,则让全选的取消
        if ($unSelectedElem.length) {
          $all.prop('checked', false);
        }
        else {
          $all.prop('checked', true);
        }
      });
    })();
  </script>
  <script type="text/javascript">
    function selectAll() {
      var ids = '';
      $(".ck").each(function() {
        if ($(this).is(':checked')) {
          ids += ',' + $(this).val(); //逐个获取id值,并用逗号分割开
      }
    });
    ids = ids.substring(1); // 进行id处理,去除第一位的逗号
    if (ids.length == 0) {
      alert('请至少选择一项');
    } else {
      if (confirm("确定删除选中的?")) {
        $.ajax({
          type: "post",
          url: "piliangdo.php",
          data: {
            ids:ids
          },
          success: function(data) {
            if(data.trim()=="yes")
            {
              alert("删除成功");
              location.reload() //刷新页面
            }
            else
            {
              alert("删除失败");
            }
          }
        });
      }
    }
  }
  </script>
</body>
</html>

piliangdo.php

<?php 
  header("content-type:text/html;charset='utf-8'");
  require_once './db_conn.php';
  
  $ids = trim($_POST['ids']);
  $ids = explode(',', $ids);
  foreach ($ids as $key => $val) {
     $del_sql = "DELETE FROM `user` WHERE id = '$val'";
     $result = mysqli_query($conn, $del_sql);
  }
  if ($result) {
    echo "yes";
  }
  else{
     echo "no";
  }
?>

php是什么语言

php,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写。PHP 是一种 HTML 内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,现在被很多的网站编程人员广泛的运用。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 使用jQuery和ajax怎么实现批量删除功能
  2. 怎么在DW中php使用批量删除注释

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php ajax jquery

上一篇:怎么在Shell脚本中使用sed命令删除特定行

下一篇:在laravel中迁移文件搜索一次删除创建字段报错如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》