-1

I'm making a shopping list, I've already have a function to delete a single item, as of right now, I'm having a problem to delete multiple items using checkbox. My item table is consist of itemName, itemId, and listId

I'm using ajax jquery

Delete Multiple JS function

function deleteMultiple(){
            $("#deleteMultiple").click(function(){
                var itemsDelete = new Array();
                $("input:checkbox:checked").each(function() {
                    itemsDelete.push( $(this).val() );
                });

                $.ajax({
                    type: "POST",
                    url: "deleteMultiple.php",
                    data: { itemsDelete : itemsDelete },
                    cache: false,
                    success: function(){
                    }
                });
                return false;

            });
        }
        }

PHP function to spit out all the items in a specific list

$sql = ("SELECT listId FROM list WHERE listName = '$listName' LIMIT 1");
$result = mysqli_query($conn,$sql) or die(mysql_error());

while($row = mysqli_fetch_assoc($result)) {
    $_SESSION['listIdIn'] = $row['listId'];
}

$sql = "(SELECT itemName FROM item WHERE listId =". $_SESSION['listIdIn'] .")";
$result = mysqli_query($conn,$sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "<li><input name = 'checkbox[]' type='checkbox' value ='" . $row["itemName"]. "'>" . $row["itemName"]. "</input><input type='button' class='deleteItem' Value='Delete'/></li>";
    }
} else {
    return null;
} 

and the PHP function to delete multiple item

$sql="SELECT * FROM item";
$result=mysqli_query($conn,$sql);

$count=mysqli_num_rows($result);

for($i=0;$i<$count;$i++){
    $itemsDelete = $_REQUEST['itemsDelete'];
    $sql = "DELETE FROM 'item' WHERE itemName = '$itemsDelete'";
    $result = mysqli_query($conn,$sql);
}

Thank you

UPDATE:So I changed my deleteMultiple.php, here's how the code looks now

$itemsDelete = $_REQUEST['itemsDelete'];
var_dump ($itemsDelete);

for ($i = 0; $i < count($itemsDelete); $i++){
    $itemsDeleteName = $itemsDelete[$i];
    $sql = "DELETE FROM 'item' WHERE 'itemName' IN (//I read from some article I have to use implode, I tried but it's not working) AND 'listId' = ".$_SESSION['listIdIn'];
    mysqli_query($conn,$sql);
}

The var_dump gives out this result

array(3) { [0]=> string(5) "item3" [1]=> string(5) "item4" [2]=> string(9) "iteeeemmm" } 

So far, by clicking the delete button, I can pass the values into multipleDelete.php, I can pass the value into php array, the problem right now, I cannot use the value in the php array and delete MySQL database

Syahnur Nizam
  • 81
  • 1
  • 12

1 Answers1

0

I'll answer one part of the issue which is clear. Please note that you may have other problems, and this is one of them for certain. In order to help you, however, you will have to post more information. Narrow down the problem, use var_dump, your browser console, etc.

In your jQuery, you are not properly setting / pushing on the right variable. Change this portion:

 // Here you declare the variable itemsDelete.  This is fine.
 var itemsDelete = new Array();       
 // Note it's preferable to declare the variable like so:    
 // var itemsDelete = [];

 $("input:checked").each(function() {
     // Here you are pushing onto an array called data.  You didn't declare that, so replace it...
     // data['itemsDelete[]'].push($(this).val());
     itemsDelete.push( $(this).val() );
  });

Your AJAX appears to be OK.

Your PHP has some typos / issues in it (in some places you use mysqli_, in others you use mysql_.

Good luck.

(Note: for why to use array literal notation when declaring arrays, see this SO answer)

Community
  • 1
  • 1
random_user_name
  • 25,694
  • 7
  • 76
  • 115