Using javascript with a link to confirm record deletion
Moderator: General Moderators
Using javascript with a link to confirm record deletion
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.
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.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Code: Select all
<a href="/delete.php?id=x" onclick="if(!confirm('are you sure?'))return false;">delete</a>
Last edited by Kieran Huggins on Thu Feb 08, 2007 5:17 pm, edited 1 time in total.
feyd | Please use
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]
[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]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 "...Kieran Huggins wrote:(returning false cancels the event)Code: Select all
<a href="/delete.php?id=x" onclick="if(!confirm('are you sure?'))return false;">delete</a>
Cheers!
confirm() returns a boolean so you can even just do:
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.
Code: Select all
<a href="url" onclick="return confirm('Are you sure?');">x</a>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.choppsta wrote:confirm() returns a boolean so you can even just do: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.Code: Select all
<a href="url" onclick="return confirm('Are you sure?');">x</a>
Cheers,
Wade
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
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