I have the following JavaScript HTML page. Its name is "Delete.php " and I'm trying to write a PHP file that can delete a file passed to it as an argument.
For example, if you run this code:
it's supposed to delete "blue.jpg", but only if you press the "Delete Button".
Unfortunately, when the file is executed, without asking anything, it deletes the file given as the argument.
How can I make a conditional statement in this JavaScript function to make it possible to choose what to do?
If the user presses "Cancel" Button, nothing should happen and the page should be redirected to "list.php".
If the user presses "Delete" Button, he/she will see a confirmation message. If he presses "OK", the file should be deleted, but if he pressed "Cancel", nothing should be deleted and the page should be redirected to "list.php".
I have even tried replacing Line 9 of the above code with this:
but it was not successful either.
Can anyone suggest a solution to this problem?
Any help will be appreciated!
For example, if you run this code:
Code:
delete.php?fname=blue.jpg
Unfortunately, when the file is executed, without asking anything, it deletes the file given as the argument.
How can I make a conditional statement in this JavaScript function to make it possible to choose what to do?
If the user presses "Cancel" Button, nothing should happen and the page should be redirected to "list.php".
If the user presses "Delete" Button, he/she will see a confirmation message. If he presses "OK", the file should be deleted, but if he pressed "Cancel", nothing should be deleted and the page should be redirected to "list.php".
Code:
<html>
<head>
<script type="text/javascript">
<!--
function confirmation() {
var answer = confirm("Delete this file? \n <?php echo $_GET["fname"]; ?>")
if (answer){
alert("File was deleted!");
<?php unlink($_GET["fname"]); ?>
window.location = "list.php";
}
else{
window.location = "list.php";
}
}
//-->
</script>
</head>
<body>
<form>
<input type="button" onclick="confirmation()" value="Delete">
<input type="button" onclick=window.location="list.php" value="Cancel">
</form>
</body>
</html>
I have even tried replacing Line 9 of the above code with this:
Code:
document.write("<?php unlink($_GET[fname]); ?>");
Can anyone suggest a solution to this problem?
Any help will be appreciated!
Comment