Stop a submit with javascript...

Posted on 2006-05-02
Last Modified: 2008-01-09
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'.

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

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

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

I suck.
Question by:mynamebecory2
    LVL 2

    Expert Comment

    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'/>

    Accepted Solution

    An even simpler way to do it would be:

    <form method='post' action='./somefile.php' onSubmit="return confirm('Are you sure?');">

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now