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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25617583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:54:48  来源:igfitidea点击:

How add confirmation box in php before deleting?

php

提问by GHugo

I create one simple list in PHP where user can add Name, age, emails, etc. I added a delete option too, but I want to add a confirmation message when the user clicks on delete option.

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

I tried searching Google but I found only jQuery and JavaScript solutions. Is there any way to do this with PHP only?

我尝试在 Google 上搜索,但只找到了 jQuery 和 JavaScript 解决方案。有没有办法只用PHP来做到这一点?

List.php

列表.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

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

回答by GHugo

If you want to do this only in PHP, you will need to add "steps" in your script, like:

如果您只想在 PHP 中执行此操作,则需要在脚本中添加“步骤”,例如:

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

To do so, you can use sessions to keep form content, and GETparameter to track the step. Otherwise the simplest solution is to use javascript:

为此,您可以使用会话来保留表单内容,并使用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!

回答by kefy

This is u need

这是你需要的

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

and create javascript function

并创建javascript函数

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

believe me it's work :)

相信我这是工作:)

回答by artie

Here is a variation of above that gives you the Confirmation box and passes a variable from PHP to Javascript and back to PHP.
I used this to select a radio button to delete a file from a list of files.
See OnClick runs function with php $fileName past to Javascript, confirmation asked with file name, and if yes, transfers to href with variables for $_GET

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

PHP/HTML Code:

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

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>

回答by Johannes Zeiser

I don't know, if this makes any sence, but I have come up with a solution on my own. It does not work, but I would like to share the idea in here, as it basically is supposed to do the same thing. My solution was to just have php echo the javascript, and then having the code, that is supposed to be executed echoed in javascript, so it would be execuded as normal php on site. this is the site where I got the idea of running the php inside the JS

我不知道这是否有意义,但我自己想出了一个解决方案。它不起作用,但我想在这里分享这个想法,因为它基本上应该做同样的事情。我的解决方案是让 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>
    ";

How I mentioned, it does not work, so I solved it just by not having confirmation.

我怎么提到的,它不起作用,所以我没有确认就解决了它。

Would be curious why this solution does not work.

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

回答by John Robertson

Add an onClickevent to trigger the dialog box and javascript:return confirm('are you sure you want to delete this?');

添加一个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>";

回答by black

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

回答by user4680654

work for me but, change this:

为我工作,但是,改变这个:

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

with:

和:

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