Anecito Alima Santillan Anecito Alima Santillan - 1 month ago 13
Ajax Question

delete record from database without refreshing

I have an code where it is supposed to delete the data without refreshing. the delete process works but i have to refresh to to remove the data.
heres my code please help me

Ajax:

$(function () {
$(".trash").click(function () {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type: "POST",
url: "delete.php", //URL to the delete php script
data: info,
success: function () {}
});
$(this).parents(".record").animate("fast").animate({
opacity: "hide"
}, "slow");
}
return false;
});
});


Here's my html:

<td style="padding-left: 23px">
<img class="photo" data-toggle="modal" data-target="#gallery<?php echo $photo; ?>" src="<?php echo $r1['photo']; ?>" />

<div class="hotel">
<button class="trash" id="<?php echo $r1['photo_id']; ?>" > <span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
</div>
</td>


If I hover the button to the image the
.trash
button will appear and if I click it the image must be deleted. help me please.

Answer

You can give a data image id attr to parent tr,

<tr data-image-id="<?php echo $r1['photo_id']; ?>">

After successful delete process (in your ajax success function) you can run code below.

$("tr[data-image-id="+del_id+"]").remove();
Comments