如何在使用PHP单击提交按钮后删除文件之前显示警告消息

时间:2014-04-11 15:20:59

标签: javascript 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">';
        echo '</form>';
        }
    }
    closedir($handle);
}

它会读取一个文件夹中的所有文件,并在屏幕上列出所有文件以及相应的处理方式。

我想在点击DELETE时显示一条消息,询问用户是否真的要删除该文件。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

<?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);
  }
?>

答案 1 :(得分:0)

您可以使用此代码:

<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>

并使用您的PHP代码:

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

或者只是使用:

echo '<input onClick="confirm(\'Are you Sure you want to delete?\')" type="image" name="del" src="img/delete.png" alt="Delete" title="Delete">';