1

I have a form which shows a file in server and gives me option to handle it like rename, delete, etc.

Here the code:

if ($handle = opendir('./uploads')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        $file = substr($file, 0, -4);

        echo '<form action="w03handling.php" method="POST">';
            echo "<input type='hidden' name='name' value='$file'>";
            echo "<input type='text' name='new_name' value='$file'>";
            echo '<input type="image" name="calc" src="img/ok.png" alt="Calculate" title="Calculate">';
            echo '<input type="image" name="rename" src="img/edit.png" alt="Rename" title="Rename">';
            echo '<input type="image" name="subst" src="img/change.png" alt="Upload new file" title="Upload new file">';
            echo '<input type="image" name="del" src="img/delete.png" alt="Delete" title="Delete">';
        echo '</form>';
        }
    }
    closedir($handle);
}

It reads all files in one folder, list them all in the screen with the respective handling possibilities.

I would like to display a message when I click on DELETE which asks the user if he really wants to delete the file.

How can I do that?

Guns
  • 2,678
  • 2
  • 23
  • 51
Aloysia de Argenteuil
  • 833
  • 2
  • 11
  • 27
  • 2
    You need JavaScript for this. Look into [confirm()](https://developer.mozilla.org/en-US/docs/Web/API/Window.confirm) – John Conde Apr 11 '14 at 15:22
  • this problem is also covered here: http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box – Carsten Hellweg Apr 11 '14 at 15:29
  • This has nothing to do with PHP, as @JohnConde suggested. PHP should only be the backend and only runs on the server side. To interact with the client, you need to use client side languages: HTML, Javascript, etc. – Devon Bessemer Apr 11 '14 at 15:29
  • @Devon [This answer](http://stackoverflow.com/a/23016805/) shows that it is possible to solve it using only PHP or at least without javascript, etc. – Aloysia de Argenteuil Apr 11 '14 at 20:03
  • @AloysiadeArgenteuil that isn't PHP. That is Javascript in HTML echoed by PHP. PHP itself isn't doing anything but echoing. – Devon Bessemer Apr 11 '14 at 21:49
  • You're right @Devon. But to say that this has nothing to do with PHP is anyway too much. We need at least PHP to echo this... ;-). And of course we could do it without PHP! Thank you! – Aloysia de Argenteuil Apr 12 '14 at 10:32

2 Answers2

3

You can try this:

<?php
if ($handle = opendir('./uploads')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
        $file = substr($file, 0, -4);

        echo '<form action="w03handling.php" method="POST">';
        echo "<input type='hidden' name='name' value='$file'>";
        echo "<input type='text' name='new_name' value='$file'>";
        echo '<input type="image" name="calc" src="img/ok.png" alt="Calculate" title="Calculate">';
        echo '<input type="image" name="rename" src="img/edit.png" alt="Rename" title="Rename">';
        echo '<input type="image" name="subst" src="img/change.png" alt="Upload new file" title="Upload new file">';
        echo '<input type="image" name="del" src="img/delete.png" alt="Delete" title="Delete" onclick=\'return confirm("Are you sure to delete this file");\'>';
        echo '</form>';
        }
    }
    closedir($handle);
  }
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Newbie
  • 287
  • 1
  • 8
0

You can use this code:

<script type="text/javascript">
function confirmdeletion() {

var r = confirm("Are you sure you want to delete?");
if (r == true)
  {
     // code that deletes all
  }
else // else part could be removed if not required
  {
     // do nothing or handle cancel
  }

}
</script>

and use your php code as :

echo '<input onClick="confirmdeletion()" type="image" name="del" src="img/delete.png" alt="Delete" title="Delete">';

OR just use:

echo '<input onClick="confirm(\'Are you Sure you want to delete?\')" type="image" name="del" src="img/delete.png" alt="Delete" title="Delete">';
Guns
  • 2,678
  • 2
  • 23
  • 51
  • 1
    [**This answer**](http://stackoverflow.com/a/23016805/) works better because it won't try and redirect after clicking on cancel. Yours works, but it wants to redirect on cancel. – Funk Forty Niner Apr 11 '14 at 15:39
  • You can remove the else part if you do not want to redirect @Fred-ii- – Guns Apr 11 '14 at 15:40
  • 1
    You could make a note of it in your answer then ;-) Just so the OP knows and is aware of it. You know and I know, but will the OP know? *ah* ;-) the $64,000 question. – Funk Forty Niner Apr 11 '14 at 15:41
  • 1
    sure @Fred-ii-. Thats a good idea! and Thanks! made the changes – Guns Apr 11 '14 at 15:41
  • I re-tested your code with OP's PHP, still the same thing (even by getting rid of the `else{}`). What did work (*or could work*) however is adding a return by changing `echo ' – Funk Forty Niner Apr 11 '14 at 15:55