I am trying to delete multiple items in the database table using checkbox. I did this with PHP, but now I want to do it with javascript so it will not refresh the page. How can I amend my code below so it will delete multiple items without refreshing page?
HTML
<div id="msg"></div>
<form>
<input type="checkbox" id='remove[]-<?php echo $item_id;?>' name="remove[]" value="<?php echo $item_id; ?>" />
<button class="btn btn-danger" id="del_btn" type="submit" name="delete"
onclick='return del(<?php echo $item_id;?>)'><b>DELETE</b></button>
</form>
JAVASCRIPT
function del(item_id){
$.ajax({
type:"post",
url:"delete_form.php",
data: {
id: item_id,
name: $('#remove[]-'+item_id).val()
},
cache:false,
success: function(html){
$('#msg').html(html);
}
});
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
}
PHP
foreach ((array) $_POST['remove'] as $remove_id) {
$delete_item = "delete from products where item_id='$remove_id'";
$run_delete = mysqli_query($con, $delete_item);
if ($run_delete){
echo "<h3> was deleted successfully</h3>";
}
}