Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript using Confirm to cancel form submission

How can I add a cancel button to an alert window?

I have used the confirm() method but when the submit button is clicked the confirm window pops up, but when clicking on cancel button, the form's data is stored.

I just want clicking on the cancel button to not store the data and leave the previous form as it is. This is my code:

function submitdata()
{   
    var r=confirm("Are You Sure You Want To Proceed?");

    if(r==true)
    {
        alert("Record is saved");
    }
    else
    {
        alert("Cancelling Transaction");
        javascript:history.go(0);
    }   
}
like image 407
Ankit Sharma Avatar asked Apr 03 '26 02:04

Ankit Sharma


1 Answers

<form action="" name="test" onsubmit="return submitdata();">
    <input type="text" />

    <input type="submit" />

</form>

<script type="text/javascript">

function submitdata() { 
    var r=confirm("Are You Sure You Want To Proceed?"); 
    if(r==true) { 
        alert("Record is saved"); 
    } else { 
        alert("Cancelling Transaction"); 
        javascript:history.go(0);
    }

}

</script>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!