I have a form with checkbox delete all option, it works fine delete items but cant remove that deleted items from the table on ajax response.
How can I remove deleted multiple ids from tables on ajax response ?
I have searched all site but there isnt a real solution for this, I tried all best answers in here and more, but none of them are the correct answer.
Note: I can remove one deleted row with $("#"+id).closest('tr').remove();
but cant delete multiple.
Best solution I found is location.reload();
page, but if reloding page then dont need ajax.
This my ajax :
$(document).on('click','[data-bs-target="#deleteAll"]',function(event){
event.preventDefault();
var del = new Array();
$("input[name='del[]']:checked").each(function(){
del.push($(this).val());
});
var valid = false;
if(confirm("Are you sure want to delete this User ? ")){
$.ajax({
url:"modules/mod_users.php",
method:"POST",
data:{del:del},
dataType:"JSON",
success:function(data){
location.reload();
valid = true;
}
});
} else {
return null;
}
});
And php part :
if(isset($_POST['del'])) {
$error = '';
$deleteAll = implode(', ', $_POST['del']);
$stmt = $pdo->prepare('SELECT * FROM users WHERE user_id IN (' . $deleteAll . ')');
$stmt->execute();
while ($row = $stmt->fetch()) {
@unlink('../images/avatars/' . $row["img"]);
}
$stmt = $pdo->prepare('DELETE FROM users WHERE user_id IN (' . $deleteAll . ')');
$stmt->execute();
if ($stmt) {
$error .= "Deleted.";
}else{
$error .= "Opps something went wrong.";
}
echo json_encode($error);
}