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.
<a href="http://stackoverflow.com"
onclick="return confirm('Are you sure?');">My Link</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
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.
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>