codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:


Home » Javascript » Article

Confirming a Delete using Javascript Confirm

Article by:  Jeff Anderson  ( 1362 ) (3/29/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: If you have a page which deletes records from a database, or indeed have any kind of link that you want the user to be doubly certain that they 'mean what they click', using the Javascript confirm event handler is a simple and effective way to get the job done.
Viewed: 99022 times Rating (3 votes): 
 5 out of 5
 Rate this Article  Read Comments  Post Comments


Confirming a Delete using Javascript Confirm



If you have a page which deletes records from a database, or indeed have any kind of link that you want the user to be doubly certain that they 'mean what they click', using the Javascript confirm event handler is a simple and effective way to get the job done. Here's the effect we're going to achieve:

Click here to return home

Assuming you're still with us and didn't after all return to the home page, you can see that the confirmation makes doubley sure you're aware of what you're doing. (Apparently some sites also use it for asking 'Are you over 18'...)

Now let's look at the script:

Select All Code


Keep all the above on one long line to ensure it works properly.

The key to this code is the phrase return confirm. Just putting confirm by itself wouldn't do much, we have to tell the browser what to do with it's confirmation. Once we tell it to return the users selection (either true for Yes or False for No), the code now simply returns false -- does nothing -- if a No is selected, and if it's true -- a Yes response -- completes the link command and zooms off to the delete page or whatever - Magic!





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Confirming a Delete using Javascript Confirm'
Posted by :  Archive Import (Matthew Dull) at 01:12 on Thursday, April 03, 2003
I needed a javascript event handler to catch my users from accidentally deleting articles from my MySQL database. But the articles' ID's were getting dynamically generated by a PHP loop, so i needed the script all on one line (inside the link, not in an outside function) so each script's action was customized. This is exactly what i needed.
Posted by :  Archive Import (James) at 09:06 on Tuesday, April 08, 2003
Just what I was looking for. Thanks!
Posted by :  Archive Import (Jeff) at 14:10 on Friday, April 11, 2003
I also had a looping function that added on ID #'s, and needed a quick confirmation... and this worked perfectly!
Posted by :  Archive Import (Natasha) at 11:55 on Thursday, June 05, 2003
Thank you. I'll make sure to bookmark this page.
Posted by :  Archive Import (Jennifer) at 12:42 on Thursday, July 17, 2003
I am having the problem where even when I click cancel on the popup confirm, it still does the action. I notice your example on this webpage does it too (still takes me to the home page when I select "Cancel"). Has anyone else noticed this?

Thanks
Posted by :  Archive Import (Velcro) at 11:48 on Monday, July 21, 2003
I used this with a Delete button and it worked perfectly. Just what I was looking for!!!!

<input type="submit" name="cmdSubmit" value=Delete onclick="javascript:return confirm('Are you sure you want to delete this POC?')">
Posted by :  Archive Import (Baub) at 09:33 on Monday, August 25, 2003
Great! Saved me a lot of time. Thank you! Thank you! Thank you!
Posted by :  Archive Import (Michele) at 17:44 on Monday, August 25, 2003
This worked perfect! Thank you!
Posted by :  AppleCoder at 18:09 on Friday, February 23, 2007
I know this is from an old article, but I am looking to find a way to change the pop up window text.

Instead of the URL of the site, I want to display certain text, can that be done?

This is what is normal

------------------------------
- http://www.codetoad.com -
- -
- You want to leave the site? -
- -
- (cancel) (ok) -
------------------------------


I want this

------------------------------
- Aria Pictures -
- -
- You want to leave the site? -
- -
- (cancel) (ok) -
------------------------------


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
Javascript - Enable and Disable form elements
by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Check IsNumeric Function
by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric.
Javascript Onload Event
by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Form Validation Function
by Jeff Anderson
A javascript validation function that you can use to validate all types of forms.
JavaScript Field Is Empty Form Validation
by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not.
Javascript Get Selected Text
by Jeff Anderson
A cross-browser script to get text selected by the user
Check Email Validation Function
by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form.
Multiple submit buttons on a single form
by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button.
Validate Form and Disable Submit Button
by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating.
Javascript onUnload Event Handler
by Jeff Anderson
The onUnload Event Handler allows you to perform an action as the user leaves the page.








Recent Forum Threads
• C++
• Re: refresh parent after closing pop up window
• Dynamic Insertion
• Date and Time function around the world???
• Significant Factors
• Perl array access
• Re: huffman encoding and decoding in C++...
• Perl One Liner: Replace {(
• Re: html including php, accessing the functions


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009