0

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.

This is my php code

<?
$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>

and this is jquery code

$(".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
});

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?

3
  • error_log(print_r($_POST,1)); so you can see what data structure you're dealing with. Commented Mar 3, 2016 at 13:34
  • I'll just drop this here as a bonus. en.wikipedia.org/wiki/Indent_style Commented Mar 3, 2016 at 13:39
  • what is alertify.success(data); mens in your code? and please show code of delete_multiple_news.php Commented Jun 26, 2020 at 1:30

2 Answers 2

1

On

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

In PHP use

$array_del = filter_input(INPUT_POST, 'array_del', FILTER_SANITIZE_STRING);

If you wanna see the result, just do:

In PHP ajax:

$array_del = filter_input(INPUT_POST, 'array_del', FILTER_SANITIZE_STRING);
echo json_encode($array_del);

In js:

$.ajax({
type: "POST",
url: "action/delete_multiple_news.php",
data: arr ,
cache: false,
success: function(data){
alertify.success(data);
console.debug(data);
//$.each(arr, function(key, value) {
//    $("tr[rel="+value+"]").fadeOut();
//});

}
});

To delete you can use WHERE in sql to filter the results who you want delete example. If the results are more than one, you make a loop or a commun column.

$mysqli->query(DELETE FROM table_name WHERE some_column = $array_del[some_column]);
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.