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

3

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

我尝试在Google上搜索,但我只找到了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>
?>

Delete.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');
          }
      }
?>
8个回答

12

如果您只想使用PHP完成此操作,您需要在脚本中添加"步骤",例如:

step1 (show form) -> step2 (ask validation) -> step3 (validate)
为此,您可以使用会话来保留表单内容,使用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!

5
这是您需要的内容。
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");
}

相信我,这很有效 :)

2

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

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

1
这是上面内容的一种变体,它提供了确认框并将变量从PHP传递到Javascript再返回到PHP。
我用它来选择要从文件列表中删除的单选按钮。
看到OnClick运行带有php $fileName的函数,询问文件名是否正确,如果是,则传输到href并使用$_GET变量。

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>


0

添加一个 onClick 事件来触发对话框和 javascript:return confirm('您确定要删除吗?');

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

0
<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()"

0

对我来说可以工作,但请更改这个:

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

使用:

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

0

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

这是我得到将php嵌入JS的想法的网站
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>
    ";

正如我所提到的,它不起作用,所以我只是通过不进行确认来解决了它。
我很好奇为什么这个解决方案不起作用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接