Чтобы удалить запись с помощью Swal (SweetAlert) JS с помощью вызова AJAX, вы можете использовать несколько методов. Вот несколько подходов:
Метод 1: использование Fetch API
Swal.fire({
title: 'Are you sure?',
text: 'This action cannot be undone.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Delete'
}).then((result) => {
if (result.isConfirmed) {
fetch('delete_record.php', {
method: 'POST',
body: JSON.stringify({ recordId: yourRecordId }),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
// Handle success response
Swal.fire('Deleted!', 'The record has been deleted.', 'success');
// Additional actions after deletion
})
.catch(error => {
// Handle error response
Swal.fire('Error!', 'An error occurred while deleting the record.', 'error');
});
}
});
Метод 2. Использование jQuery AJAX
Swal.fire({
title: 'Are you sure?',
text: 'This action cannot be undone.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Delete'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'delete_record.php',
type: 'POST',
dataType: 'json',
data: { recordId: yourRecordId },
success: function(data) {
// Handle success response
Swal.fire('Deleted!', 'The record has been deleted.', 'success');
// Additional actions after deletion
},
error: function(xhr, status, error) {
// Handle error response
Swal.fire('Error!', 'An error occurred while deleting the record.', 'error');
}
});
}
});
Не забудьте заменить 'delete_record.php'соответствующим серверным скриптом, который обрабатывает операцию удаления, а 'yourRecordId'фактическим идентификатором записи, которую вы хотите удалить.