WWW FAQs: How can I ask the user to confirm before submitting a form?

2004-09-03: sometimes you want to make sure the user really intended to click on a form button, or even on an ordinary link. Examples include "delete" buttons.

In the most important cases, you should solve this problem by adding an additional "confirmation" page to the dynamic web application that receives the form submission. Since not all users have JavaScript enabled in their browsers, there is a chance that your confirmation prompt will not be seen before the form is submitted or the link is followed.

In situations where this possibility is acceptable, or you cannot modify the CGI, PHP, ASP or other script accepting the form submission, you can use a simple JavaScript technique to confirm the click. If the user does not click "Yes," the form submission or link navigation is canceled. See below for a simple example of a link with a confirmation prompt:

<a href="scarypage.html"
 onClick="return confirm('Do you really want to follow ' +
  'this link? It's really scary!');">
Vist Our Scary Page</a>

Also, see below for an example of a form submission with a confirmation prompt:

<form method="POST" action="submit.php">
<input type="hidden" name="accountid" value="23948234">
<input type="submit" name="delete"
 value="Delete My Account"
 onClick="return confirm(
  'Are you sure you want to delete your account?');">

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!