Using javascript with a link to confirm record deletion

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Using javascript with a link to confirm record deletion

Post by Wade »

Hi again everyone,

What I'm trying to do is be able to have a delete link next to a record listing, when the user clicks the link have a pop-up confirm that was the intended action, if so then proceed to delete the record.

Currently I have another page that has a button on it, that will do a js confirm and that works great, but I can't seem to figure out how to do it via a link...

Any suggestions are appreciated.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

<a href="/delete.php?id=x" onclick="if(!confirm('are you sure?'))return false;">delete</a>
(returning false cancels the event)
Last edited by Kieran Huggins on Thu Feb 08, 2007 5:17 pm, edited 1 time in total.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[syntax="html"]
<input type="button" name="btndelete" value="DELETE SELECTED" onClick="if (ew_confirm('<?php echo "Delete Records?"; ?>')) {this.form.action=page.php';this.form.encoding='application/x-www-form-urlencoded';this.form.submit();}">

<script type="text/javascript">
// Confirm Message
function ew_confirm(msg)
{
	var agree=confirm(msg);
	if (agree)
		return true ;
	else {
		return false ;
	}
}
</script>

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

Kieran Huggins wrote:

Code: Select all

<a href="/delete.php?id=x" onclick="if(!confirm('are you sure?'))return false;">delete</a>
(returning false cancels the event)
Works like a charm! Thank you! I had something similar before but it kept throwing errors, finally figured out I had to escape all the "...

Cheers!
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

confirm() returns a boolean so you can even just do:

Code: Select all

<a href="url" onclick="return confirm('Are you sure?');">x</a>
As I mentioned in another post this very same day, using a GET request for an action that changes something like this is a bad idea. It should really be a POST request.
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

choppsta wrote:confirm() returns a boolean so you can even just do:

Code: Select all

<a href="url" onclick="return confirm('Are you sure?');">x</a>
As I mentioned in another post this very same day, using a GET request for an action that changes something like this is a bad idea. It should really be a POST request.
How could you do a POST from a link like that? And why is the $_GET a bad idea? I'm pretty new to php so just asking to make sure I (try) to understand all the concepts.

Cheers,

Wade
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

You could use javascript to submit a hidden form.

It's all to do with RESTful principles http://www.google.co.uk/search?hl=en&q= ... principles.
This page has a lot of useful info: http://ajaxpatterns.org/RESTful_Service ... or_queries
Post Reply