Confirm Before A Form Submit June 22, 2024 Post a Comment 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> CopyJavaScript:var el = document.getElementById('myCoolForm'); el.addEventListener('submit', function(){ returnconfirm('Are you sure you want to submit this form?'); }, false); CopyEdit: 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>CopySolution 2: <inputtype="submit" onclick="return confirm('Are you sure you want to do that?');"> CopySolution 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 }); }); CopyReference: 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; } }Copy<formaction="admin.php"method="POST"><inputtype="submit"value="Submit" /></form>Copy Share Post a Comment for "Confirm Before A Form Submit"
Post a Comment for "Confirm Before A Form Submit"