Вот живая демонстрация: Codepen demo
Во-первых, убедитесь, что вы не дублируете html идентификаторы -> используйте class=""
вместо id=""
для похожих элементов:
<!-- OLD:
<a href="process.php?delete_id=<?php echo $news['crimenews_id'];?>" class="btn btn-danger" id="Test" style="border:2px solid black;"><b>Delete <i class="fas fa-trash-alt"></i></b></a>
-->
<!-- new: -->
<a href="#" data-target="<?php echo $news['crimenews_id'];?>" class="prompt-delete btn btn-danger" style="border:2px solid black;"><b>Delete <i class="fas fa-trash-alt"></i></b></a>
Обратите внимание, чтобы быть уверенным, что будет использоваться окно подтверждения, я изменил href
на # и поместил атрибут crimenews_id
в data-
.Затем обновите ваш сценарий, чтобы он предлагал пользователю перед удалением удалить элемент:
<script>
$(".prompt-delete").click(function() {
// get the target id to delete
var target_id = $(this).data("target");
// prepare the url to redirect in case of deleting an item
var url_delete = "process.php?process.php?delete_id=" + target_id;
// initialize confirmation box
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
type: "warning",
showCancelButton: true,
allowOutsideClick: true,
allowEscapeKey: true,
confirmButtonColor: "btn btn-primary",
cancelButtonColor: "btn btn-danger",
confirmButtonText: "Sure",
onClose: function(e) {
console.log(e);
}
}).then(result => {
if (result.value) {
// if CONFIRMED => go to delete url
window.location = url_delete;
}
});
});
</script>