如何在删除之前在php中添加确认框


How add confirmation box in php before deleting?

我在PHP中创建了一个简单的列表,用户可以在其中添加姓名、年龄、电子邮件等。我也添加了一个删除选项,但我想在用户单击删除选项时添加一条确认消息。

我试着在谷歌上搜索,但只找到了jQuery和JavaScript解决方案。有没有什么方法可以只使用PHP来做到这一点?

List.php

<?php
   include('config.php');
   $query1=mysql_query("select id, name, age from addd");
   echo "<table><tr><td>Name</td><td>Age</td><td></td><td></td>";
   while($query2=mysql_fetch_array($query1))
        {
           echo "<tr><td>".$query2['name']."</td>";
           echo "<td>".$query2['age']."</td>";
           echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
           echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
        }
    </table>
?>

删除.php

<?php
    include('config.php');
    if(isset($_GET['id']))
      {
        $id=$_GET['id'];
        $query1=mysql_query("delete from addd where id='$id'");
        if($query1)
          {
          header('location:list.php');
          }
      }
?>

如果你只想在PHP中完成这项工作,你需要在脚本中添加"步骤",比如:

step1 (show form) -> step2 (ask validation) -> step3 (validate)

为此,您可以使用sessions来保存表单内容,并使用GET参数来跟踪步骤。否则,最简单的解决方案是使用javascript:

echo "<td><a onClick='"javascript: return confirm('Please confirm deletion');'" href='delete.php?id=".$query2['id']."'>x</a></td><tr>"; //use double quotes for js inside php!

这是你需要的

while($query2=mysql_fetch_array($query1))
{
     echo "<tr><td>".$query2['name']."</td>";
     echo "<td>".$query2['age']."</td>";
     echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
     echo "<td><a onclick='javascript:confirmationDelete($(this));return false;' href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}

并创建javascript函数

function confirmationDelete(anchor)
{
   var conf = confirm('Are you sure want to delete this record?');
   if(conf)
      window.location=anchor.attr("href");
}

相信我,这是有效的:)

这里是上面的一个变体,它为您提供了确认框,并将一个变量从PHP传递到Javascript,然后再传递回PHP
我用它来选择一个单选按钮,从文件列表中删除一个文件
请参阅OnClick运行函数,将php$fileName传递给Javascript,要求使用文件名进行确认,如果是,则使用$_GET 的变量转移到href

PHP/HTML代码:

<?php
echo "
    <tr>
        <td>
            <input type='radio' name='input_sched_button'  
            onclick='confirmation(".'"'.$fileName.'"'.")' />
        </td>
        <td class='fixed-td-450'><a href='./$namehref'>$fileName</a></td>
        <td class='fixed-td-40'>$extn</td>
        <td class='col3'>$size</td>
        <td class='fixed-td-80'>$modtime</td>
    </tr>
";
?>

Javascript

<script>
    function confirmation(delName){
    var del=confirm("Are you sure you want to delete this record?'n"+delName);
    if (del==true){
        window.location.href="Some_Program.php?delete=y&file="+delName;
    }
    return del;
}
</script>

onclick="return confirm('你确定吗?')"

<a href="post-delete.php?delete=<?php echo $id; ?>" onclick="return confirm('Are You Sure ?')">delete</a>

添加onClick事件以触发对话框和javascript:return confirm('are you sure you want to delete this?');

echo "<td><a href='delete.php?id=".$query2['id']."' onClick='"javascript:return confirm('are you sure you want to delete this?');'">x</a></td><tr>";
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
   alert ("record deleted")
}
return del;
}
</script>
//add onclick event
onclick="return deleletconfig()"

对我有用,但请更改此项:

onclick='javascript:confirmationDelete($(this));return false;'

带有:

onclick='confirmationDelete(this);return false;'

我不知道这是否有意义,但我已经自己想出了一个解决方案。它不起作用,但我想在这里分享这个想法,因为它基本上应该做同样的事情。我的解决方案是让php回显javascript,然后让应该在javascript中执行的代码回显,这样它就可以在现场作为普通php执行。这是我想到在JS 中运行php的地方

echo "
    <script>
    if (window.confirm('möchten Sie die Datei wirklich unwiderruflich löschen?')){
        window.alert('<?php
        unlink($allFiles);
        rmdir($fileDir));
    ?>');
    }
    else{
    window.alert('Vorgang abgebrochen');
    }
    </script>
    ";

我提到的,它不起作用,所以我只是通过没有确认来解决它。

很好奇为什么这个解决方案不起作用。