6

I have a page on my website that is dynamically created with information from an SQL database. As well as the data being displayed a delete link is also created for each record which links to a php file called deleteRecord.php that deletes that record from the database.

Example of data listings

Is there any way I can incorporate a confirmation message so that when the Delete link is clicked it will only run the deleteRecord.php file if the response is Yes?

2

7 Answers 7

39

You could use JavaScript. Either put the code inline, into a function or use jQuery.

  1. Inline:

    <a href="deletelink" onclick="return confirm('Are you sure?')">Delete</a>
    
  2. In a function:

    <a href="deletelink" onclick="return checkDelete()">Delete</a>
    

    and then put this in <head>:

    <script language="JavaScript" type="text/javascript">
    function checkDelete(){
        return confirm('Are you sure?');
    }
    </script>
    

    This one has more work, but less file size if the list is long.

  3. With jQuery:

    <a href="deletelink" class="delete">Delete</a>
    

    And put this in <head>:

    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script language="JavaScript" type="text/javascript">
    $(document).ready(function(){
        $("a.delete").click(function(e){
            if(!confirm('Are you sure?')){
                e.preventDefault();
                return false;
            }
            return true;
        });
    });
    </script>
    
5
  • Excellent - thanks for your help. However in section 2 I you have two <script> lines. I didn't need these, just one <script>
    – RGriffiths
    Oct 1, 2013 at 7:53
  • @RichardGriffiths Yeah, I had accidentally put the jQuery <script> there. Moved it to #3. Oct 1, 2013 at 16:57
  • In a PHP environment, the inline version may fail to work, throwing a syntax error. A simple workaround is to escape the single quotes in the confirmation message text: onclick="return confirm(\'Are you sure?\')
    – Dut A.
    Nov 28, 2015 at 8:27
  • <a onclick="return confirm(\'Are you sure ?\')" href="'.base_url().'admin/delete_client/'.$id.'" id="'.$id.'" class="delete_client"><i class="fa fa-trash-o"></i></a>
    – Learner
    Jul 19, 2016 at 13:57
  • Fastest, easiest solution. Thank you!
    – Narc0t1CYM
    Aug 19, 2016 at 9:17
2

You have 2 options

1) Use javascript to confirm deletion (use onsubmit event handler), however if the client has JS disabled, you're in trouble.

2) Use PHP to echo out a confirmation message, along with the contents of the form (hidden if you like) as well as a submit button called "confirmation", in PHP check if $_POST["confirmation"] is set.

2

Call this function onclick of button

/*pass whatever you want instead of id */
function doConfirm(id) {
    var ok = confirm("Are you sure to Delete?");
    if (ok) {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                window.location = "create_dealer.php";
            }
        }

        xmlhttp.open("GET", "delete_dealer.php?id=" + id);
        // file name where delete code is written
        xmlhttp.send();
    }
}
0

You can do it with an confirm() message using Javascript.

0

Try this one :

 <script type="text/javascript">
      var baseUrl='http://example.com';
      function ConfirmDelete()
      {
            if (confirm("Delete Account?"))
                 location.href=baseUrl+'/deleteRecord.php';
      }
  </script>


  echo '<a type="button" onclick="ConfirmDelete()">DELETE ACCOUNT</a>';
0

<?php 
$con = mysqli_connect("localhost","root","root","EmpDB") or die(mysqli_error($con));
 if(isset($_POST[add]))
 {
	$sno = mysqli_real_escape_string($con,$_POST[sno]);
   $name = mysqli_real_escape_string($con,$_POST[sname]);
   $course = mysqli_real_escape_string($con,$_POST[course]);
 
   $query = "insert into students(sno,name,course) values($sno,'$name','$course')";
   //echo $query;
   $result = mysqli_query($con,$query);
   printf ("New Record has id %d.\n", mysqli_insert_id($con));
  mysqli_close($con);
   
 }	 
?>
  
<html>
<head>
<title>mysql_insert_id Example</title>
</head>
<body>
<form action="" method="POST">
Enter S.NO: <input type="text" name="sno"/><br/>
Enter Student Name: <input type="text" name="sname"/><br/>
Enter Course: <input type="text" name="course"/><br/>
<input type="submit" name="add" value="Add Student"/>
</form>
</body>
</html>

0

Another method using both onlcick & form submit button with php record id value (record to be delete). Use php code to get the record ID to be deleted. This is working for me.

<form action="deleteRecord.php" method="POST">

    <button onclick="return confirm('Are you sure! want to delete?')" type="submit" name="id" value="<?=$record['id'];?>" >Delete</button>

</form>

deleteRecord.php example file
<?php 
    $con=mysqli_connect("localhost","root","","dbname") or die(mysqli_error($con));
    if(isset($_POST['id']))
    {
        $id = mysqli_real_escape_string($conn, $_POST['id']);
        $query = "DELETE FROM table_name WHERE id='$id' ";
        $query_run = mysqli_query($conn, $query);
    }
    mysqli_close($con);

    if($query_run)
    {
        echo "Deleted Successfully";
        exit(0);
    }
        else
    {
        echo "Not Deleted";
        exit(0);
    }
?>
        

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.