20

I have searched for an answer but couldn't find one!

I have a simple form,

<form action="adminprocess.php" method="POST">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>

How would I adjust this to confirm before processing the form?

I tried onclick, but couldn't get it working.

Any ideas?

UPDATE - What I now have.

<script type="text/javascript">
var el = document.getElementById('myCoolForm');

el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
</script>

<form action="adminprocess.php" method="POST" id="myCoolForm">
     <input type="submit" name="completeYes" value="Complete Transaction" />
</form>
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • 1
    what was the onclick code that you had? – AllisonC Jun 27 '11 at 12:45
  • You have to add the listener **after** the form is in the document. Use window.onload to add it, or put the script after the form in the page. – RobG Jun 27 '11 at 13:26
  • Wasn't aware it had to come afterwards, but it makes sense. Thanks RobG. – sark9012 Jun 27 '11 at 14:47
  • [javascript form submit](https://stackoverflow.com/q/6515502/6521116) – LF00 Jun 01 '17 at 04:36
  • Does this answer your question? [JavaScript Form Submit - Confirm or Cancel Submission Dialog Box](https://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box) – Flimm Nov 11 '21 at 09:30

8 Answers8

53

HTML:

<form action="adminprocess.php" method="POST" id="myCoolForm">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>

JavaScript:

var el = document.getElementById('myCoolForm');

el.addEventListener('submit', function(){
    return confirm('Are you sure you want to submit this form?');
}, false);

Edit: you can always use inline JS code like this:

<form action="adminprocess.php" method="POST" onsubmit="return confirm('Are you sure you want to submit this form?');">
    <input type="submit" name="completeYes" value="Complete Transaction" />
</form>
Michael J.V.
  • 5,499
  • 1
  • 20
  • 16
19
<input type="submit" onclick="return confirm('Are you sure you want to do that?');">
Greenisha
  • 1,417
  • 10
  • 14
2

The correct event is onSubmit() and it should be attached to the form. Although I think it's possible to use onClick, but onSubmit is the correct one.

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • 1
    There is not onsubmit event. There is an HTML attribute is named *onsubmit*, but the related event is the [submit](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-htmlevents) event. – RobG Jun 27 '11 at 13:31
  • ok, this is about semantics, as onclick is not event as well, but I agree with you, as you are indicating the correct terms – Maxim Krizhanovsky Jun 27 '11 at 13:44
1

If you're already using jQuery.. you can use an event handler to trigger before submission

$(document).ready(function() {
   $("#formID").submit(function(){
      // handle submission
   });
});

Reference: http://api.jquery.com/submit/

Atticus
  • 6,585
  • 10
  • 35
  • 57
  • Not using jQuery, but thankyou for the suggestion. Definitely something I need to learn more about. – sark9012 Jun 27 '11 at 12:50
  • I would strongly suggest using jQuery if you are going to be developing with javascript. It provides a platform on which many functions you may have to create yourself, are already created. And its very extendable. – Atticus Jun 27 '11 at 12:51
  • Any suggestions on the best place to get to grips with it? Quite simply the jQuery website? – sark9012 Jun 27 '11 at 12:52
0

var submit = document.querySelector("input[type=submit]");
  
/* set onclick on submit input */   
submit.setAttribute("onclick", "return test()");

//submit.addEventListener("click", test);

function test() {

  if (confirm('Are you sure you want to submit this form?')) {         
    return true;         
  } else {
    return false;
  }

}
<form action="admin.php" method="POST">
  <input type="submit" value="Submit" />
</form>
antelove
  • 3,216
  • 26
  • 20
0

In my case, I didn't have a form ID and couldn't add inline in the form tag. I ended up with the following jQuery code

    var form = $("form").first();
    form.on('submit', function() {
        return confirm('Are you sure you want to submit this form?');
    });
Rami Alloush
  • 2,308
  • 2
  • 27
  • 33
0
document.getElementById("myCoolForm").addEventListener('submit', function(event){
            event.preventDefault();
           
            var message = "Are you sure you want to perform this action?";
            
             if (confirm(message)) {
                    document.getElementById('myCoolForm').submit();
                    return true;
             }
             return false;
        });
Godstime John
  • 253
  • 3
  • 8
-2

if you have more then one submit buttons that do different actions you can do it this way.

<input TYPE=SUBMIT NAME="submitDelete"  VALUE="Delete Script" onclick='return window.confirm("Are you sure you want to delete this?");'>
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Dextr
  • 9