17
<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')">
<input type='submit' name='delete' value='Undo' />
<input type='submit' name='no' value='No' />

when the user clicks on second submit button i.e No i want to display the confirmation dialogue as "Are you sure you want to commit the transaction."

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
RatDon
  • 3,403
  • 8
  • 43
  • 85
  • 1
    possible duplicate of [Javascript onsubmit with form with multiple submits buttons](http://stackoverflow.com/questions/13707633/javascript-onsubmit-with-form-with-multiple-submits-buttons) – totymedli Aug 03 '13 at 23:15

5 Answers5

30
<form method='post'>
    <input type='submit' name='undo' value='Undo' onclick="return confirm('Are you sure you want to rollback deletion of candidate table?')"/>
    <input type='submit' name='no' value='No' onclick="return confirm('Are you sure you want to commit delete and go back?')"/>
</form>

Worked fine. just changed onsubmit() to onclick(). as the function of both in this situation is same.

RatDon
  • 3,403
  • 8
  • 43
  • 85
  • 3
    This solution isn't as useful if there is validation on the form that would interrupt the post of the form an the onclick will fire even when the form doesn't post. You could still use it if you call your validation as part of the onclick though. – mikeschuld Aug 10 '18 at 16:47
2

You could bind to onclick instead of onsubmit - see below.

<script> 
function submitForm() {
    return confirm('Rollback deletion of candidate table?');
}
<script>

<form>
    <input type='submit' onclick='submitForm()' name='delete' value='Undo' />
    <input type='submit' onclick='submitForm()' name='no' value='No' />
</form>

Or alternately, using jQuery:

<script> 
$(document).ready(function() {
    $('form input[type=submit]').click(function() {
        return confirm('Rollback deletion of candidate table?');
    });
});
<script>
jpm
  • 16,622
  • 34
  • 63
  • 66
Madison May
  • 2,723
  • 3
  • 22
  • 32
0
<form onsubmit="submitFunction();">
    <input type='submit' name='delete' value='Undo' />
    <input type='button' onclick="declineFunction()" name='no' value='No' />
</form>

I wouldnt try to create a submit but rather just a button that has a onclick="function()" and then use javascript to set a variable to see how many times they have clicked it and a alert();

hope this helps :D

0

Here's an event-listener based solution that avoids inline event handlers (useful if your site has a Content Security Policy that forbids inline JavaScript):

HTML:

<form method="post">
    <input type="submit" id="deleteButton" name="delete" value="Undo" />
    <input type="submit" id="noButton" name="no" value="No" />
</form>

JS:

document.getElementById("deleteButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to rollback deletion of candidate table?")) { 
        evt.preventDefault();
    }
});

document.getElementById("noButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to commit the transaction?")) { 
        evt.preventDefault();
    }
});
S. Jensen
  • 94
  • 4
-1

Just use two of the same form. One for each button:

<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')">
    <input type='submit' name='delete' value='Undo' />
</from>
<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')">
    <input type='submit' name='no' value='No' />
</from>

Also, if you would done your research you would find these:

Community
  • 1
  • 1
totymedli
  • 29,531
  • 22
  • 131
  • 165