Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirm button before running deleting routine from website

Tags:

html

php

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?

like image 971
RGriffiths Avatar asked Sep 26 '13 07:09

RGriffiths


People also ask

How to use confirm in PHP?

Confirm dialog box displays a predefined message with two buttons: OK and Cancel buttons. The user will have to click either of the button to proceed. If the user clicks an OK button, the box returns true to the program. If the user clicks the Cancel button, the box returns false to the program.

Are you sure you want to delete in Javascript?

Use Window confirm() method in the client-side to confirm delete before delete in JavaScript. When you want to verify the user or delete something, it always a good idea to confirm the request before processing.

How use Javascript confirm in PHP?

“confirm box javascript in php” Code Answer'secho "<td><a onClick=\"javascript: return confirm('Please confirm deletion');\" href='delete. php? id=". $query2['id']."'>x</a></td><tr>"; //use double quotes for js inside php!


2 Answers

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>
    
like image 128
PurkkaKoodari Avatar answered Sep 28 '22 21:09

PurkkaKoodari


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.

like image 43
Zack Newsham Avatar answered Sep 28 '22 19:09

Zack Newsham