Page 1 of 1
Confirm Delete
Posted: Fri Apr 28, 2006 2:02 pm
by psurrena
Anyone have a simple solution to confirm a delete?
Code: Select all
echo "<a href=del_n.php?id=" . $id . ">delete</a>";
Thanks
Posted: Fri Apr 28, 2006 2:10 pm
by hawleyjr
Confirm a delete of what? A record, a file? a table? We prob. need more on this one...
Edit: Are you talking about Javascript?
Posted: Fri Apr 28, 2006 2:18 pm
by psurrena
Sorry, of a record. Is javascript the only option for displaying a dialog box? Or can it be done in just PHP?
Posted: Fri Apr 28, 2006 2:30 pm
by php3ch0
javascript:
Code: Select all
function confirm_del(id) {
if(confirm("Are you sure you want to delete this \n This cannot be undone")) {
window.location= id
}
}
Posted: Fri Apr 28, 2006 2:47 pm
by Nathaniel
psurrena wrote:Sorry, of a record. Is javascript the only option for displaying a dialog box? Or can it be done in just PHP?
Yes, PHP can do it.
Pseudocode:
Code: Select all
if ( mode == delete )
{
if ( confirm == true )
{
// delete record
}
else
{
// display a confirm button that links to page.php?mode=delete&confirm=true (for example)
}
}
Posted: Fri Apr 28, 2006 7:17 pm
by Ollie Saunders
Code: Select all
echo '<a onclick="return confirm(\'Sure?\')" href="del_n.php?id=' . $id . '>delete</a>';
Posted: Sat Apr 29, 2006 2:15 am
by Chris Corbyn
ole wrote:Code: Select all
echo '<a onclick="return confirm(\'Sure?\')" href="del_n.php?id=' . $id . '>delete</a>';
Exactly... the confirm dialogue can pass back a false return value to your hyperlink which renders it inactive.
Posted: Sat Apr 29, 2006 3:10 am
by Ollie Saunders
Yep, can't get much simplier. No functions, logic or external code required.
Posted: Sat Apr 29, 2006 6:41 am
by timvw
Only the code by Nathaniel would work for people that have disabled javascript too...
Posted: Sat Apr 29, 2006 8:00 am
by Ollie Saunders
ole wrote:Code: Select all
echo '<a onclick="return confirm(\'Sure?\')" href="del_n.php?id=' . $id . '>delete</a>';
Will work without showing the confirm dialog for users without JavaScript
Posted: Sat Apr 29, 2006 10:18 am
by timvw
But the code wouldn't ask users without javascript for confirmation... It would simply delete. (I admit that my phrasing 'will not work for ppl without javascript' wasn't good and should have been more like 'will not ask for confirmation if ppl don't have javascript enabled')
Nathaniel's code on the other hand clearly suggested another variable ($confirm) from a (separate) confirmation form...
Posted: Sat Apr 29, 2006 1:34 pm
by Ollie Saunders
There is no longer any dispute as to what onclick="return... does when JavaScript is on and off.
In many circumstances no confirmation will be an acceptable fallback for non-JS users especially considering it will be so much faster for those who do have JS then a server side confirmation but yes server side confirmation is the only one that will work all the time.
Posted: Sat Apr 29, 2006 9:39 pm
by feyd
Considering the application would perform quite differently, potentially doing very bad things, based on my Javascript being enabled or not, I'd consider that a bug. Ole may not, but psurrena may. That is up to him/her, but it is something to be aware of and consider for sure.
Posted: Sat Apr 29, 2006 10:05 pm
by santosj
Create a JavaScript Function that passes another get variable to the string that would tell the Server Script that it was confirmed by the JS (not secure). The other way would be to use the JavaScript HTTPXMLRequest(), which would be more secure.
If you use the JavaScript function and they don't have JavaScript enabled, then it would just go to the url which the server would ask if they are sure they want to delete the record.
The same thing as with the AJAX method, if JavaScript is enabled it would pop up and if confirmed send the request to the file without reloading the page. If not, then same as above.
Problem solved.