如何使用php和ajax删除多个记录 [英] How to delete multiple records using php and ajax

查看:119
本文介绍了如何使用php和ajax删除多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用php显示新闻时,我会有多行新闻.我正在尝试使用每行旁边的复选框同时删除多行.

I have multiple rows of news when I show them using php. I'm trying to remove multiple rows simultaneously using checkboxes next to each row.

这是我的php代码

<?
$select_news = $mysqli->query("SELECT * FROM news order by time desc limit $x,$numbershownews");

            while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){

            $id_news       = $rows_news ['id'];
            $title_news    = $rows_news ['title'];
        ?>
        <table>
        <tr rel="<? echo $id_news; ?>">

        <td class="tdtable" id="<? echo $id_news; ?>" width="4%">
        <input type="checkbox" rel="<? echo $id_news; ?>" name="checkbox[]" class="checkboxtable" value="<? echo $id_news; ?>">
        </td>
        <td class="tdtable" id="<? echo $id_news; ?>" width="50%">
        <? echo  $title_news; ?>
        </td>
        </tr
        </table
    <?
    }
    ?>
<button class="deletecheckednews">Delete</button>

这是jquery代码

$(".deletecheckednews").on( 'click', function () {

  var arr = new Array();

  $(".checkboxtable:checked").each(function () {
    arr.push($(this).attr("rel"));
  });

  if(arr == "") {
    alertify.error("no selected news"); 
  } else {

    $.ajax({
      type: "POST",
      url: "action/delete_multiple_news.php",
      data: arr ,
      cache: false,
      success: function(data) {
        alertify.success(data);

        $.each(arr, function(key, value) {
          $("tr[rel="+value+"]").fadeOut();
        });
      }
    });

  }

  return false
});

如何将该数组传递给"delete_multiple_news.php"?我已经尝试过此$checkedneww = $_REQUEST['arr'];和此$checkedneww = $_POST['arr'];,但是它不起作用.我将如何删除选定的行?

How can I pass this array on to "delete_multiple_news.php"? I have tried this $checkedneww = $_REQUEST['arr']; and this $checkedneww = $_POST['arr']; but it doesn't work. How would I go about removing the selected rows?

推荐答案

您正在ajax调用(data: arr)中传递原始数组,但是期望使用命名变量($_POST['arr']).

You're passing a raw array in your ajax call (data: arr) but you're expecting a named variable ($_POST['arr']).

我不确定jquery是否可以使用data: arr(多年以来都没有使用过),但是如果可以,那么您要查找的ids数组整体上就是$_POST.

I'm not sure if data: arr is possible with jquery (haven't used it in years) but if it is, then the ids array you're looking for is simply $_POST in its whole.

如果不是这种情况,或者您想执行$_POST['arr'],则应将JS更改为data: {arr: arr}.

If that's not the case, or you want to do $_POST['arr'] you should change your JS to data: {arr: arr}.

这篇关于如何使用php和ajax删除多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆