3

I tried to find out where is the problem, but no clue.
I have 4 buttons for one form by using HTML5 multi submit. When I click on DELETE button, a dialog pops out, by using an attribute onclick="confirm('message');. When I hit Cancel, it should stop form submission, close the dialog and stay on the same page without any actions, instead it keeps submitting the form. It works perfectly until now, and I can't find out where is the problem.

<form action="http://google.com/index/25" method="post" accept-charset="utf-8">
    <button class="btn" type="submit" name="formSubmit" value="new">New</button>
    <button class="btn" type="submit" name="formSubmit" value="save">Bulk save</button>
    <button class="btn btn-danger" type="submit" name="formSubmit" value="delete" onclick="confirm('Do you really want to remove a record(s)?.');">Delete permanently</button>
    <button class="btn" type="submit" onclick="confirm('stop or proceed');">submit</button>
</form>

And a live DEMO IS HERE on jsbin

Anyone got the same bug ?

aspirinemaga
  • 3,251
  • 6
  • 41
  • 79
  • possible duplicate of [JavaScript Form Submit - Confirm or Cancel Submission Dialog Box](http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box) – bfavaretto Dec 08 '13 at 16:17

2 Answers2

7

Handle it on the form instead of the button, and have the handler return the outcome from the confirm dialog:

<form onsubmit="return confirm('stop or proceed');">

You can also handle the events of each button, but don't forget to return:

<button onclick="return confirm('blabla');">Button</button>
bfavaretto
  • 69,385
  • 15
  • 102
  • 145
  • Hm, try that code with return directly on the button, but I'm not sure it's gonna work. If not, you can make each button set a flag you can check from the form's onsubmit. – bfavaretto Dec 08 '13 at 16:15
  • But that's what I already done, isn't it ? My handler is not working, because of the `confirm()` dialog ignores it. It works some couple days before, I don't know what it can be – aspirinemaga Dec 08 '13 at 16:17
  • 1
    You are not returning the outcome of confirm (at least not on the code you posted). – bfavaretto Dec 08 '13 at 16:20
  • Oh yes, I've missed a `return` before the `confirm()`, now it works, please could you update your answer but `` instead of `
    `.
    – aspirinemaga Dec 08 '13 at 16:28
1

If you wanna use Jquery, you should use this code:

<a href="www.blahblah.com" class="classTest">click</a>

JQuery

$(function () {
$(".classTest").click(function (e) {
            return confirm("Submit the Post action?");
 });
}
LucaGuerra
  • 179
  • 1
  • 10