Reputation: 123
I was using AJAX with Laravel and was trying to create a CRUD operation for practice where i was using sweetalert box to display a note before a field actually gets deleted from the database. The SWAL alertbox says "Do you want to delete" and if the user clicks "YES", the field should get deleted else nothing should happen. In my case, No matter what I click, be it the "YES" button or the "NO" button or even if i click away from the alert window, it just deleted the entry from the database no matter what. Below is my code snippet PS: I AM USING AXIOS TO SENT REQUESTS TO SERVER
deleteUser(id) {
// Handles user deletion
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
this.form.delete('api/user/' + id)
.then(() => {
// Fire the manipulated user event
Fire.$emit('ManipulateUser');
// do something if request was successfull
if(result.value) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
}).catch(() => {
// do something if request fails
});
})
}
Upvotes: 0
Views: 323
Reputation: 628
You need to add
Swal.fire({
.
.
.
}).then((result) => {
if (result.value) { // This check here. This contains value for the delete button. Its null for cancel button
...
}
})
You are sending delete request without any check on it. So apply this check before the delete call
Upvotes: 1