Confirm Delete

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Confirm Delete

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Confirm a delete of what? A record, a file? a table? We prob. need more on this one... :roll:


Edit: Are you talking about Javascript?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

Sorry, of a record. Is javascript the only option for displaying a dialog box? Or can it be done in just PHP?
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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
} 
}
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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)
  }
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

echo '<a onclick="return confirm(\'Sure?\')" href="del_n.php?id=' . $id . '>delete</a>';
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yep, can't get much simplier. No functions, logic or external code required.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Only the code by Nathaniel would work for people that have disabled javascript too...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post 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.
Post Reply