For example, if you run this code:
Expand|Select|Wrap|Line Numbers
- 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".
Expand|Select|Wrap|Line Numbers
- <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:
Expand|Select|Wrap|Line Numbers
- document.write("<?php unlink($_GET[fname]); ?>");
Can anyone suggest a solution to this problem?
Any help will be appreciated!