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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James
    New Member
    • Jun 2011
    • 17

    #1

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

    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

    Code:
    echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';

    this is the delete.php where the delete happens

    Code:
    <?php
    
    	include('connect.php');
    
    		if (isset($_GET['id']) && is_numeric($_GET['id']))
    			{
    				$id = $_GET['id'];
    
    				$result = mysql_query("DELETE FROM house_info WHERE id=$id")
    				or die(mysql_error()); 
    
    				header("Location:admin.php");
    			}
    			else
    				{
    					header("Location:admin.php");
    				}
    
    ?>
    and I was thinking of using this JavaScript code but it doesn't work

    Code:
    <a href="javascript:confirmation(<? echo $row['id']; ?>)">delete</a>
    
    <script type="text/javascript">
    <!--
    function confirmation(ID) {
    	var answer = confirm("Delete entry "+ID+" ?")
    	if (answer){
    		alert("Entry Deleted")
    		window.location = "delete.php?act=trackdelete&id="+ID;
    	}
    	else{
    		alert("No action taken")
    	}
    }
    //-->
    </script>
    Any feedback would be much appreciated.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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?

    Comment

    • James
      New Member
      • Jun 2011
      • 17

      #3
      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?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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.

        Comment

        • Marknut
          New Member
          • Apr 2010
          • 42

          #5
          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:
          Code:
          echo '<td><a href="javascript:void(0);" onclick="confirmation(' . $row['id'] . ');">Delete</a>';
          JS:
          Code:
          function confirmation(ID) { 
              if(!confirm("Delete entry "+ID+" ?")) { 
                  return false;
              }
              window.location = "delete.php?act=trackdelete&id="+ID;
          }

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            that would fail completely (i.e. not deleting) if JS is disabled

            Comment

            • Marknut
              New Member
              • Apr 2010
              • 42

              #7
              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.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I don’t. matter of un-annoyance and cautiousness. because I know what JS is capable of.

                Comment

                • James
                  New Member
                  • Jun 2011
                  • 17

                  #9
                  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.
                  Last edited by James; Dec 24 '11, 03:21 PM. Reason: to provide a clearer understanding

                  Comment

                  Working...