简体   繁体   中英

How to submit a form using sweet alert confirm?

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.

  • Press submit
  • Show sweet alert
  • IF yes is clicked = send form
  • If cancel is clicked = close sweet alert

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">
                    &nbsp;||&nbsp;
                    <input type="submit" id="btn_submit" name="btn_submit" class="btn btn-success" value="Save">
                </td>
            </tr>
            </table>
 </form>

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;
      }
    });
});

you can get your form element by Id then submit.

document.getElementById("frm_input_srt").submit();

or jquery way

$("#frm_input_srt").submit();
 }, function () { 
 //to submit:
 document.getElementById("frm_input_srt").submit();
 });

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.
        });
});

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();
    }
});

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM