By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,639 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,639 IT Pros & Developers. It's quick & easy.

how to display a prompt when user chooses to delete a record in php

P: 17
Hi guys.

I have an admin section on my website and I would like to display a question asking the user if they would really like to delete a record once clicked. I am using PHP and MySQL to delete records from a database but if somebody accidentally clicks the delete link then it deletes straight away. Would I need to use a JavaScript prompt message?

This is the delete link

Expand|Select|Wrap|Line Numbers
  1. echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
  2.  

this is the delete.php where the delete happens

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     include('connect.php');
  4.  
  5.         if (isset($_GET['id']) && is_numeric($_GET['id']))
  6.             {
  7.                 $id = $_GET['id'];
  8.  
  9.                 $result = mysql_query("DELETE FROM house_info WHERE id=$id")
  10.                 or die(mysql_error()); 
  11.  
  12.                 header("Location:admin.php");
  13.             }
  14.             else
  15.                 {
  16.                     header("Location:admin.php");
  17.                 }
  18.  
  19. ?>
  20.  
  21.  
and I was thinking of using this JavaScript code but it doesn't work

Expand|Select|Wrap|Line Numbers
  1.  
  2. <a href="javascript:confirmation(<? echo $row['id']; ?>)">delete</a>
  3.  
  4. <script type="text/javascript">
  5. <!--
  6. function confirmation(ID) {
  7.     var answer = confirm("Delete entry "+ID+" ?")
  8.     if (answer){
  9.         alert("Entry Deleted")
  10.         window.location = "delete.php?act=trackdelete&id="+ID;
  11.     }
  12.     else{
  13.         alert("No action taken")
  14.     }
  15. }
  16. //-->
  17. </script>
  18.  
Any feedback would be much appreciated.
Dec 22 '11 #1

✓ answered by Marknut

I know this one is already answered, so feel free to ignore this reply. This code is essentially what you were doing initially, but I've implimented an identical solution, so I thought I would post.

PHP:
Expand|Select|Wrap|Line Numbers
  1. echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row['id'] . ');">Delete</a>';
JS:
Expand|Select|Wrap|Line Numbers
  1. function confirmation(ID) { 
  2.     if(!confirm("Delete entry "+ID+" ?")) { 
  3.         return false;
  4.     }
  5.     window.location = "delete.php?act=trackdelete&id="+ID;
  6.  

Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
delete lines #3 & #16 from your JavaScript. they are unnecessary.

this should also show up in the Error Console as syntax error.

besides that. what do you do in case JS is disabled by intention?
Dec 22 '11 #2

P: 17
hmmmm good point Dormilich I didn't think of this. Do you know of a simple way to display a prompt should a user click a delete link by mistake?
Dec 22 '11 #3

Dormilich
Expert Mod 5K+
P: 8,639
if JS is disabled, there is no way. however there are 2 possible ways to handle that
a) load a confirm page. optionally, you may skip this if the user presses OK from the comfirm dialog.
b) make an UNDO/REDO system. i.e. you don’t delete the data, you just disable the regular availability. you don’t need to fear that your DB will get too big, DBs can hold millions of data sets without losing performance.
Dec 22 '11 #4

P: 42
I know this one is already answered, so feel free to ignore this reply. This code is essentially what you were doing initially, but I've implimented an identical solution, so I thought I would post.

PHP:
Expand|Select|Wrap|Line Numbers
  1. echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row['id'] . ');">Delete</a>';
JS:
Expand|Select|Wrap|Line Numbers
  1. function confirmation(ID) { 
  2.     if(!confirm("Delete entry "+ID+" ?")) { 
  3.         return false;
  4.     }
  5.     window.location = "delete.php?act=trackdelete&id="+ID;
  6.  
Dec 23 '11 #5

Dormilich
Expert Mod 5K+
P: 8,639
that would fail completely (i.e. not deleting) if JS is disabled
Dec 23 '11 #6

P: 42
That's correct, javascript functions fail if javascript is disabled. Of course best practice is to develop for the most basic scenario and then add functionality for more advanced technology. Aside from that...the vast majority (~98%) of users have javascript enabled.
Dec 23 '11 #7

Dormilich
Expert Mod 5K+
P: 8,639
I don’t. matter of un-annoyance and cautiousness. because I know what JS is capable of.
Dec 23 '11 #8

P: 17
Thanks Marknut for that post. I was going to decide against providing the option to OK or cancel when a user wishes to delete a record, but then I saw your reply. Your post wasn't ignored at all and thanks for replying. I am now using your code.
Dec 24 '11 #9

Post your reply

Sign in to post your reply or Sign up for a free account.