Skip to content Skip to sidebar Skip to footer

Confirm Before A Form Submit

I have searched for an answer but couldn't find one! I have a simple form,

Solution 1:

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(){
    returnconfirm('Are you sure you want to submit this form?');
}, false);

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

<formaction="adminprocess.php"method="POST"onsubmit="return confirm('Are you sure you want to submit this form?');"><inputtype="submit"name="completeYes"value="Complete Transaction" /></form>

Solution 2:

<inputtype="submit" onclick="return confirm('Are you sure you want to do that?');">

Solution 3:

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.

Solution 4:

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/

Solution 5:

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

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

  if (confirm('Are you sure you want to submit this form?')) {         
    returntrue;         
  } else {
    returnfalse;
  }

}
<formaction="admin.php"method="POST"><inputtype="submit"value="Submit" /></form>

Post a Comment for "Confirm Before A Form Submit"