Confirm button before running deleting routine from website
Yes, you can add a confirm button before running a delete routine on a website using PHP. Here is an example of how this can be done:
- Create a delete button or link that, when clicked, will trigger the delete routine. For example, you can create a link with the text "Delete" and an id of "delete_button".
<a href="#" id="delete_button">Delete</a>
- Use JavaScript to attach an event listener to the delete button. When the button is clicked, the event listener will display a confirm dialog box asking the user to confirm or cancel the delete action.
document.getElementById("delete_button").addEventListener("click", function(event){
event.preventDefault();
if(confirm("Are you sure you want to delete this item?")){
// Run the delete routine
}
});
Watch a video course
Learn object oriented PHP
- To run the delete routine, you can use an ajax call to the PHP script that performs the delete action.
if(confirm("Are you sure you want to delete this item?")){
$.ajax({
url:"delete_script.php",
type:"post",
data:{
id: item_id
},
success: function(response) {
if(response=="success"){
//Do something like removing the item from the page
}
}
});
}
- In the PHP script, you can use the received id to delete the item from the database
<?php
$id = $_POST['id'];
//Delete query here
echo "success";
?>
This is a basic example and can be modified to fit the specific requirements of your website.