473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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.  

8 9527
Dormilich
8,658 Expert Mod 8TB
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
James
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
8,658 Expert Mod 8TB
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
Marknut
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
8,658 Expert Mod 8TB
that would fail completely (i.e. not deleting) if JS is disabled
Dec 23 '11 #6
Marknut
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
8,658 Expert Mod 8TB
I don’t. matter of un-annoyance and cautiousness. because I know what JS is capable of.
Dec 23 '11 #8
James
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

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

Similar topics

8
by: Ian Davies | last post by:
Hello I am trying to run a few INSERT queries only if the user clicks 'continue' in a <a href> The queries takes variables created from SELECT queries. I have tried to acheive this by putting The...
3
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to...
6
by: JHNielson | last post by:
This is a very simple question.... I have a form that looks up Records for an unbound drop-down list. It has worked just fine up until last night. Now the button on the form to delete a record...
1
by: kang jia | last post by:
hi now i would like user to delete what they don;t want to buy after they already put it in shopping cart. i would like to have a pop up box to alert users that they are going to delete what they...
4
by: Craggy | last post by:
Hi, I am trying to pop up a yes/no message box so that a user can delete a record in a continuous form. The default delete message is a bit sloppy because it seems to move the continuous form to...
1
by: THEAF | last post by:
i'm trying to create a reminder form, when i add a new reminder its ok but when i try to delete a reminder then i get No Current Record problem. Form Load Private Sub Form_Load() Dim s() As...
1
by: CreativeMind | last post by:
hi all, private void dgMygrid_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { Button bDelete=(Button)e.Item.FindControl("btnDelete"); if(bDelete!=null) {...
8
by: bluemoon9 | last post by:
Does anyone know how to write a code to promt the selection at the last record in the list box?. I have this code below in the "On Click" event, when user click on the code, it close a form, requery...
1
by: Karan Singh Goyal | last post by:
I have an access table which have fields like Customer Id, First Name, Last Name etc. i know how to generate auto number but when i delete last record it generates next number instead of previous one...
2
by: ALIYU HABIB | last post by:
I want to write a code on username and password authentication, but i can't display "*" when entering the password. I am using turbo c++ compiler.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.