Search code examples
javascripthtmlformsform-submitconfirmation

Submit HTML form with Javascript link as well as confirm box


I am trying to submit form data, but instead of using the standard "Submit" button I want to use a regular HTML link. However, when I click this link I want to have a confirmation box asking the user to continue or cancel. I have both parts of the puzzle, but when I put them together they don't play nice. This is what I currently have for Javascript code:

function confirmSubmit()
{
var agree=confirm("Are you sure you wish to delete this file?");
if (agree)
    return true ;
else
    return false ;
}
function submitform() {
    document.forms["delete14"].submit();
}

And for the HTML I have:

<a href="javascript:submitform()" onClick="javascript:confirmSubmit()">link text</a>

The confirmation box does pop up as intended and the form does submit properly. But currently the form submits even if the user clicks "no" on the confirmation box.


Solution

  • Try this:-

    function confirmSubmit()
    {
    var agree=confirm("Are you sure you wish to delete this file?");
    if (agree)
        document.forms["delete14"].submit();
    else
        return false ;
    }