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.
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?
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.
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.
“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!
You could use JavaScript. Either put the code inline, into a function or use jQuery.
Inline:
<a href="deletelink" onclick="return confirm('Are you sure?')">Delete</a>
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With