javascripthtmlformssubmitconfirm

JavaScript Form Submit - Confirm or Cancel Submission Dialog Box


For a simple form with an alert that asks if fields were filled out correctly, I need a function that does this:

I think a JavaScript confirm would work but I can't seem to figure out how.

The code I have now is:

function show_alert() {
  alert("xxxxxx");
}
<form>
  <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" alt="PayPal - The safer, easier way to pay online!" value="Submit">
</form>


Solution

  • A simple inline JavaScript confirm would suffice:

    <form onsubmit="return confirm('Do you really want to submit the form?');">
    

    No need for an external function unless you are doing validation, which you can do something like this:

    <script>
    function validate(form) {
    
        // validation code here ...
    
    
        if(!valid) {
            alert('Please correct the errors in the form!');
            return false;
        }
        else {
            return confirm('Do you really want to submit the form?');
        }
    }
    </script>
    <form onsubmit="return validate(this);">