Reputation: 542
I'm trying to submit a form after confirming it using sweet alert. But, somehow my form is bypassing the sweet alert confirm (sent without confirmation).
What I am trying to do is.
Here's my script
$("#btn_submit").on('click',function(e){ //also can use on submit
e.preventDefault(); //prevent submit
var jns_srt = $("#i_dok").val();
swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
cancelButtonText: "Cancel",
closeOnConfirm: true
}, function () {
//I don't know what to write here.
});
});
<form name="frm_input" id="frm_input_srt" method="post" action="<?php echo site_url('Admin/rekam_srt_msk'); ?>">
<table>
<tr><td colspan="3" style="text-align:left; padding:0; vertical-align:middle"><input type="text" name="test" /></td></tr>
<tr>
<td colspan="3" style="text-align:center; padding:0; vertical-align:middle">
<input type="reset" id="btn_reset" name="btn_reset" class="btn btn-danger" value="Reset">
||
<input type="submit" id="btn_submit" name="btn_submit" class="btn btn-success" value="Save">
</td>
</tr>
</table>
</form>
Upvotes: 1
Views: 18044
Reputation: 347
UPD: 2021
$("#frm_input_srt")
.submit(async () => {
const jns_srt = $("#i_dok").val();
const result = await swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
cancelButtonText: "Cancel",
closeOnConfirm: true
});
if(!result.isConfirmed)
event.preventDefault();
});
Upvotes: 0
Reputation: 432
This is your answer
("#btn_submit").on('click',function(e){ //also can use on submit
e.preventDefault(); //prevent submit
swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
cancelButtonText: "Cancel",
closeOnConfirm: true
}
}).then(function(value) {
if (value) {
$('#frm_input_srt').submit();
}
});
Upvotes: 0
Reputation: 1
Use this:
$("#btn_submit").on('click',function(e){ //also can use on submit
e.preventDefault(); //prevent submit
var jns_srt = $("#i_dok").val();
swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
cancelButtonText: "Cancel",
closeOnConfirm: true
}, function (getAction) {
if (getAction.value === 'true') {
// Insert code that will be run when user clicks Ok.
});
});
Upvotes: 0
Reputation: 7295
The documentation shows how to run different code on cancel and confirm. Straight from their site:
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
For your code:
$("#frm_input_srt").submit( function () {
var jns_srt = $("#i_dok").val();
swal({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes!",
cancelButtonText: "Cancel",
closeOnConfirm: true
}, function(isConfirm){
if (isConfirm) {
return true;
} else {
return false;
}
});
});
Upvotes: 1
Reputation:
}, function () {
//to submit:
document.getElementById("frm_input_srt").submit();
});
Upvotes: 0
Reputation: 553
you can get your form element by Id then submit.
document.getElementById("frm_input_srt").submit();
or jquery way
$("#frm_input_srt").submit();
Upvotes: 1