I have a button Delete File in script1.php
.
After clicking on that button, I wan to display a JS confirm
box to confirm the action.
I'm using this code :
<script language="javascript">
if (confirm("Are you sure to delete this file ?")) {
// Will continue executing the script
} else {
window.stop();
}
</script>
<?php
// Delete the file ...
?>
This code does what I want : The php
won't execute unless the user confirms the action.
Now, if I want to customize the confirm box
(Add another button, modify the style ...), I thought of using jQuery .dialog
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="dialog-confirm" title="Do you wanna continue ?">
<p>Are you sure to delete this file ?</p>
</div>
<script language="javascript">
$( document ).ready(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Do it": function() {
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
window.stop();
}
}
});
});
</script>
<?php
// Delete the file ...
?>
Unfortunately, this does not work, and the rest of the php code will be executed any way.
So, is there a way to "halt" the execution til the click on the "Do it" button?