Last active
February 4, 2019 06:51
-
-
Save khanzadimahdi/c31a2d3309e2227b1bae8fb8ac329cff to your computer and use it in GitHub Desktop.
laravel on delete request ask yes-no question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//use jquery and sweetAlert2 | |
$(document).ready(function() { | |
$("form input[name~=_method][value~=DELETE]").closest('form').submit(function(event) { | |
event.preventDefault(); | |
var form = $(this); //wrap this in jQuery | |
swal({ | |
title: "are you sure?", | |
text: "do you realy want to delete this item?", | |
type: "warning", | |
showCancelButton: true, | |
closeOnConfirm: false, | |
showLoaderOnConfirm: true, | |
confirmButtonText: 'yes, remove it', | |
cancelButtonText: 'no, i dont want', | |
}, function (isConfirm) { | |
if (isConfirm) { | |
$.ajax({ | |
method: 'POST', | |
url:form.attr('action'), | |
data:{ | |
_token: $('meta[name="csrf-token"]').attr('content'), | |
_method: 'DELETE', | |
}, | |
headers: { | |
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') | |
}, | |
success: function(){ | |
if(form.closest('tr').length>0){ | |
form.closest('tr').css('background-color','#00A').fadeOut(); | |
}else{ | |
location.reload(); | |
} | |
swal({ | |
title: "removing", | |
text: "data has been removed successfully", | |
type:'success', | |
}); | |
}, | |
error: function(){ | |
swal({ | |
title: "removing", | |
text: "removing data failed, please try again later.", | |
type: 'error', | |
}); | |
} | |
}); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add this js in your pages that has delete form.
it will recognize all of your forms that has delete request and sets an event listener for them (using input with _method="delete").