Javascript - How to cancel a Post form with a confirmation message?

1.8k Views Asked by At

Here is the code to Receive the POST :

{{velocity}}
#if ($request.getMethod() == "POST")
$request.getParameter("servicename")
#end
{{/velocity}}

Here is the code to Send the POST :

{{velocity}}

{{html}}

Here is the code for the javascript :

<script type="text/javascript">
function validateForm()
{

c = confirm("Are you sure ?");
if(c == true){
alert("The foo has been created");
}else{
alert("The foo creation has been canceled.");
return false;
}
}
</script>

Here is the code for the html form :

<form name="myForm" action="$doc.getURL('view')" method="post" onsubmit="return  validateForm()">
<input type="hidden" name="xaction" value="createservice" />
 Foo Name:
<input type="text" name="servicename" />
 <input type="submit" value="Create" />
 </form>
{{/html}}

 {{/velocity}}
1

There are 1 best solutions below

0
On

I don't know if this will help you but the following code works:

<html>
<head>
<script type="text/javascript">

function foo() {
    if (confirm("Are you sure?")) {
        alert("OK");
        return true;
    }
    else {
        alert("KO");
        return false;
    }
}

</script>
</head>
<body>

<form method="post" onsubmit="return foo()">
    <input type="text" />
    <input type="submit" value="Go" />
</form>

</body>
</html>

And then you can get rid of foo:

<form method="post" onsubmit="return confirm('Are you sure?')">