Stop a submit with javascript...

I have a form in PHP that will submit $_POST to a script for processing.  Right now I have a confirmation box (javascript) that pops up when the 'submit' button is clicked asking 'Are you sure?', but either way the form gets processed . . . How can I stop the form from being processed if the user clicks 'Cancel'.




HTML/PHP
<form method='post' action='./somefile.php'>

....form inputs....
<input type='submit' onClick='confirmSubmit()' />
</form>

JS
function confirmSubmit(){
if(confirm('Are you sure?')){
return true;
} else { return false;}
}


I suck.
LVL 1
mynamebecory2Asked:
Who is Participating?
 
jim_spathConnect With a Mentor Commented:
An even simpler way to do it would be:

<form method='post' action='./somefile.php' onSubmit="return confirm('Are you sure?');">
0
 
schalcraftCommented:
You are so close to getting it! You need to add the validation as a onSubmit action for your form, rather than as a button action.

<form method='post' action='./somefile.php' onSubmit="return confirmSubmit(this)'>

....form inputs....
<input type='submit'/>
</form>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.