Page 1 of 1

Using javascript with a link to confirm record deletion

Posted: Thu Feb 08, 2007 5:04 pm
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.

Posted: Thu Feb 08, 2007 5:16 pm
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)

Posted: Thu Feb 08, 2007 5:16 pm
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]

Posted: Fri Feb 09, 2007 10:07 am
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!

Posted: Fri Feb 09, 2007 10:28 am
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.

Posted: Fri Feb 09, 2007 10:32 am
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

Posted: Fri Feb 09, 2007 10:40 am
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