views:

2756

answers:

6

My php page has a link to delete one mysql table data.i want it to be in such a way that when i click the 'delete' link a confirm box should appear and it shoul ask for "are you sure, you want to delete?" with two buttons 'yes' & 'no'.when i click yes ,i want to delete the mysql table's data,and when i click no,nothing should happen. please get me an answer...thank u.

+9  A: 
<a href="http://stackoverflow.com" 
    onclick="return confirm('Are you sure?');">My Link</a>
Tim S. Van Haren
+2  A: 

You can use JavaScript to prompt you:

Found this here - Example

<script>
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete")) {
   document.location = delUrl;
  }
}
</script>

<a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>

Another way

<a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>

Warning: This JavaScript will not stop the records from being deleted if they just navigate to the final url - delete.page?id=1 in their browser

Nick Clarke
A: 

The PHP way of doing this would be using a dialog system inside php.for example GTKDialogs: http://www.kksou.com/php-gtk2/articles/setup-a-dialog-box---Part-2---simple-yes-no-dialog.php The javacript way is probably a bit easier, but remember when javascript is turned off, this does not work (except if you check javascript to be enabled and then add this!?) This could be with a onclick handler like tsvanharen posted, or with a simple text dialog inside the page instead of a nagging popup.

<a onClick="$('deletefromtable').show();"></a>
<div id="deletefromtable" style="display:none;">
 Do you really want to do this?<br/>
 <a href="deleteit.php">Yes</a>|<a onClick="$('deletefromtable').hide();">No</a>
</div>

I use prototype (hence the $() tag and the show()/hide()) for it. but you can easily change it to work without prototype:

<a onClick='document.getElementById("deletefromtable").style.display = "block";' href="#">click here to delete</a>
<div id="deletefromtable" style="display:none;">
 Do you really want to do this?<br/>
 <a href="deleteit.php">Yes</a>|<a onClick='document.getElementById("deletefromtable").style.display = "none";' href="#">No</a>
</div>

Again, this does not work without javascript, but almost no options do.

xaddict
Even if phpgtk was installed on the server (which is nonsense, as it requires a graphical interface, that is never installed on web servers running Linux), it would open that dialog box on the server, and not inside the browser nor on the user's machine : you cannot, from the server, open a GTK window on the client
Pascal MARTIN
+4  A: 

You should always use a form/post button to confirm something like this. Never rely on Javascript. Read this to understand why!

Mez
A: 

If you're deleting something, you should always use POST and not GET, so using confirm() is a bad idea. What I do in your situation is use a modal popup like jqModal and display a form with yes/no buttons inside the popup.

jimyi
+1  A: 

What I usually do is create a delete page that shows a confirmation form if the request method is "GET" and deletes the data if the method was "POST" and the user chose the "Yes" option.

Then, in the page with the delete link, I add an onclick function (or just use the jQuery confirm plugin) that uses AJAX to post to the link, bypassing the confirmation page.

Here's the idea in pseudo code:

delete.php:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['confirm'] == 'Yes') {
        delete_record($_REQUEST['id']); // From GET or POST variables
    }
    redirect($_POST['referer']);
}
?>

<form action="delete.php" method="post">
    Are you sure?
    <input type="submit" name="confirm" value="Yes">
    <input type="submit" name="confirm" value="No">

    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
    <input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
</form>

Page with delete link:

<script>
    function confirmDelete(link) {
        if (confirm("Are you sure?")) {
            doAjax(link.href, "POST"); // doAjax needs to send the "confirm" field
        }
        return false;
    }
</script>

<a href="delete.php?id=1234" onclick="return confirmDelete(this);">Delete record</a>
Matthew Crumley