Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 1st, 2009, 06:14 PM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default multiple actions from one onclick

I'm trying to put out a confirmation box, and then go to a new page if the user clicks on OK. Seems simple but when I click OK, it doesn't go anywhere. Obviously my Javascript skills are lacking for this. Here's my code:

Code:
<input name="Delete" type="button" value="Delete" 
     onclick="javascript:return confirm('Are you sure you want to delete this member?');location.href='delete.cfm'">
I'd like to keep this in the onclick if possible. Can someone advise why it isn't going to the location.href after I click OK?
 
Old January 1st, 2009, 11:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

return means, in all computer languages I'm aware of (maybe 12 or 15), "return RIGHT NOW and do NOT do ANYTHING past this point."

So when the user says "OK" to your confirm, WHAM, you are done, right then.

The funny part: If the user does *NOT* say "OK" then your code will go on and do the change to the location.href!

In other words, your code is doing the *opposite* of what you want it to do.

So you could fix this a couple of ways, but I'd prefer seeing you do this:
Code:
<input name="Delete" type="button" value="Delete" 
     onclick="if ('Are you sure you want to delete this member?') {location.href='delete.cfm';}">
p.s.: I do NOT see how changing the page to delete.cfm is going to cause deletion of this specific record. You will be passing *NOTHING* to that new page that will tell it WHICH member you want to delete. You don't show enough code to know this for sure, but if the above code doesn't work, I'm willing to be that is the problem. In which case show more code. Say, from the <FORM> to the </FORM> if it's not too much code. (If it's too much, try to toss out anything irrelevant to the delete, per se.)
 
Old January 1st, 2009, 11:28 PM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default That didn't work either

Sorry that I'm not good at javascript syntax. I can program in a variety of languages but I use javascript pretty rarely and as a result am always uncomfortable when I try to craft a bit of code. The example I was giving came from a coldfusion application I have so I intentionally left out a few of the things that might just muddy the water.

I found a number of examples on the internet that showed me that :

Code:
onclick="javascript:return confirm('Are you sure you want to delete this member?');"
would produce the box and ask the question I wanted (in fact I use this approach in some asp.net apps I developed). But because I wanted to have the OK response move on to a new page I found other examples that implied I could add the next action. Apparently I didn't realize the syntax meant return unconditionally. Incidentally when I clicked cancel it didn't go to delete.cfm either.

OK, I made the code as you suggested:
Code:
onclick="if ('Are you sure you want to delete this member?') {location.href='delete.cfm';}"
and it went directly to delete.cfm without asking the question. I also tried

Code:
onclick="if confirm('Are you sure you want to delete this member?') {location.href='delete.cfm';}"
and it did nothing.

Sorry to be so dense, but javascript is not my thing. I'm just trying to insert a fragment that works into my coldfusion web page.

Wouild appreciate some more assistance.
 
Old January 1st, 2009, 11:45 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It was just a simple oversight within the if statement. Your code should read:

html Code:
<input name="Delete" type="button" value="Delete"
     onclick="if (confirm('Are you sure you want to delete this member?')) {location.href='delete.cfm';}">

I have tested this and, on button click, the user is presented with an alert box. If they click 'Ok' they are sent to the delete.cfm page otherwise the page does nothing.


hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
The Following User Says Thank You to dparsons For This Useful Post:
jpullam (January 2nd, 2009)
 
Old January 2nd, 2009, 04:11 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yeah, sorry about that. I shortened it TOO much. DOH.

However, my comment about the fact that I don't think it's going to *DO* anything when you get to that "delete.cfm" page still stands. Because you are passing *NOTHING* to it. No id, no state information, etc., etc.

*IF* you are showing only ONE thing (one record) that could be deleted--and if CF is remembering what that ONE thing is--then it could work. But that's about the only way.
 
Old January 2nd, 2009, 09:00 AM
Authorized User
 
Join Date: Oct 2006
Posts: 20
Thanks: 1
Thanked 0 Times in 0 Posts
Default Works fine now

No problem. Glad to get it working.

FYI, the actual code fragment (not simplified) is

Code:
<input name="Delete" type="button" value="Delete" onclick="if (confirm('Are you sure you want to delete this member?')) {location.href='memdelete.cfm?ID=#URL.ID#';}">
I didn't want to get sidetracked into a discussion of the Coldfusion notation or the next set of questions about the integrity of passing the variable in a URL, asession variable. etc., so I trimmed the code down for discussion purposes.
 
Old January 2nd, 2009, 08:37 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

AHA! That is *EXACTLY* what I was referring to!
?ID=#URL.ID#

You probably won't be too surprised to know that in ASP or JSP code one might well use
?ID=<%=URL.ID%>

Yes, of course, passing the id of the record to be deleted is exactly what was missing. Pardon my paranoia. <grin style="sickly" />





Similar Threads
Thread Thread Starter Forum Replies Last Post
Focus on Actions Panel Stela Flash (all versions) 1 September 29th, 2007 11:45 PM
trigger actions b00gieman Beginning VB 6 1 September 25th, 2007 07:23 AM
e-mail actions... ruhin Beginning PHP 4 May 4th, 2005 10:52 AM
User Actions ppenn Access VBA 1 May 18th, 2004 02:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.