24

I have a form and want to intercept form submission to display a confirmation dialog using bootbox.

  1. User enters some data
  2. User hits submit
  3. A confirmation dialog is shown

If the user hits OK, then the form should submit, if not it should just stay on the page.

I tried this:

$('#myForm').submit(function() {
    return bootbox.confirm("Are you sure?");
});

However, bootbox.confirm() immediately returns, the confirmation dialog is hidden again.

I then noticed that there is a callback parameter on bootbox.confirm(). However, if I'm to call $('#myForm').submit() from the callback, this will obviously just show the confirmation dialog again.

So what is the proper way to confirm form submission?

apex39
  • 603
  • 1
  • 6
  • 20
theDmi
  • 17,546
  • 6
  • 71
  • 138
  • 1
    Btw, changing the submit button to an ordinary one and attaching the script there is no solution, because the user can still trigger form submission by pressing enter. – theDmi Jul 03 '12 at 14:58

5 Answers5

45

I only got this working by preventing the default form behaviour, here is the solution below. I'm using this in a list of forms, each one with a submit delete button and it works fine.

<script type="text/javascript">
    $('form').submit(function(e) {
        var currentForm = this;
        e.preventDefault();
        bootbox.confirm("Are you sure?", function(result) {
            if (result) {
                currentForm.submit();
            }
        });
    });
</script>
Nuno Reis
  • 574
  • 5
  • 5
12

As always: Just after you ask a question, you find the answer yourself :-) . See this answer: https://stackoverflow.com/a/2420806/219187 (also note the comment in the answer).

Adapted to my problem, the solution looks as follows:

$('#myForm').submit(function() {
    var currentForm = this;
    bootbox.confirm("Are you sure?", function(result) {
        if (result) {
            currentForm.submit();
        }
    });
});
Community
  • 1
  • 1
theDmi
  • 17,546
  • 6
  • 71
  • 138
1

This code works perfectly for me...

<script type="text/javascript">
$('#myForm').click(function(e) {
    e.preventDefault();
    var msg = 'Souhaitez vous vraiment supprimer ce produit ?';
    bootbox.confirm(msg, function(result) {
        if (result) {
            $('#myForm').submit();
        }
    });
});
</script>
1

You can a solve this by passing additional parameters to your submit handler using the .trigger() method:

$('form').submit(function (e, confirmed) {
    var currentForm = $(e.currentTarget);
    if (!confirmed) {
        e.preventDefault();
        bootbox.confirm("Are you sure?", function (result) {
            if (result) {
                currentForm.trigger('submit', { confirmed: true });
            }
        });
    }
});

When the form is first submitted, the 'confirmed' parameter is undefined and the bootbox dialog is displayed. If the user confirms the action in the dialog, submit event is triggered again with the additional parameter and the form is submitted normally.

0

My solution

@Html.ActionLink("Delete", "DeleteReport", new { id = item.Id }, new { @class = "btn btn-danger", onclick = String.Format("return ASE.ConfirmAction(this.href, 'Delete {0}?');", item.Name) })
var ASE = {
    ConfirmAction: function (href, text) {
        bootbox.confirm(text, function (result) {
            if (result)
                window.location = href;
        });

        return false;
    }
}
Alexandr Sulimov
  • 1,894
  • 2
  • 24
  • 48