I want to show a Sweet Alert 2 confirm message to user after submitting the form like this:
<form id="myForm" data-flag="0" action="" method="POST">
@csrf
<input type="checkbox" id="btn-submit" name="wallet_checked" onchange="this.form.submit();">
</form>
As you can see I have used onchange="this.form.submit();"
to submit the form without using submit button and it works fine.
Then I tried adding this as script for showing SweetAlert message to user:
$(document).on('click', '#btn-submit', function(e) {
e.preventDefault();
let form = $(this).parents('form');
swal(
{
title: "Attention!",
text: "Are you sure you want to make this",
type: "warning",
allowEscapeKey: false,
allowOutsideClick: false,
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
showLoaderOnConfirm: true,
closeOnConfirm: false
},
function (isConfirm) {
if (isConfirm) {
console.log("YES");
}
return false;
});
});
So the function shows Sweet Alert message on screen after clicking on the checkbox, but when I press on Yes button, it does not run console.log("YES");
.
And this means that if (isConfirm) {
at function (isConfirm) {
is not working properly.
So how to solve this issue? What is wrong with isConfirm
?
Change this to :
$(document).on('click', '#btn-submit', function(e) {
e.preventDefault();
let form = $(this).parents('form');
swal(
{
title: "Attention!",
text: "Are you sure you want to make this",
type: "warning",
allowEscapeKey: false,
allowOutsideClick: false,
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
showLoaderOnConfirm: true,
closeOnConfirm: false
})
.then((isConfirm) => {
if (isConfirm) {
console.log("YES");
}
return false;
});
});