php 在从网站运行删除例程之前确认按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19022176/
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 18:40:38  来源:igfitidea点击:

Confirm button before running deleting routine from website

phphtml

提问by RGriffiths

I have a page on my website that is dynamically created with information from an SQL database. As well as the data being displayed a delete link is also created for each record which links to a php file called deleteRecord.php that deletes that record from the database.

我的网站上有一个页面,该页面是使用 SQL 数据库中的信息动态创建的。除了显示的数据外,还会为每个记录创建一个删除链接,该链接链接到一个名为 deleteRecord.php 的 php 文件,该文件从数据库中删除该记录。

Example of data listings

数据列表示例

Is there any way I can incorporate a confirmation message so that when the Delete link is clicked it will only run the deleteRecord.php file if the response is Yes?

有什么方法可以合并一条确认消息,以便在单击“删除”链接时,如果响应为“是”,它只会运行 deleteRecord.php 文件?

回答by PurkkaKoodari

You could use JavaScript. Either put the code inline, into a function or use jQuery.

你可以使用 JavaScript。要么将代码内联,放入一个函数中,要么使用 jQuery。

  1. Inline:

    <a href="deletelink" onclick="return confirm('Are you sure?')">Delete</a>
    
  2. In a function:

    <a href="deletelink" onclick="return checkDelete()">Delete</a>
    

    and then put this in <head>:

    <script language="JavaScript" type="text/javascript">
    function checkDelete(){
        return confirm('Are you sure?');
    }
    </script>
    

    This one has more work, but less file size if the list is long.

  3. With jQuery:

    <a href="deletelink" class="delete">Delete</a>
    

    And put this in <head>:

    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script language="JavaScript" type="text/javascript">
    $(document).ready(function(){
        $("a.delete").click(function(e){
            if(!confirm('Are you sure?')){
                e.preventDefault();
                return false;
            }
            return true;
        });
    });
    </script>
    
  1. 排队:

    <a href="deletelink" onclick="return confirm('Are you sure?')">Delete</a>
    
  2. 在一个函数中:

    <a href="deletelink" onclick="return checkDelete()">Delete</a>
    

    然后把它放进去<head>

    <script language="JavaScript" type="text/javascript">
    function checkDelete(){
        return confirm('Are you sure?');
    }
    </script>
    

    这个有更多的工作,但如果列表很长,文件大小会更少。

  3. 使用 jQuery:

    <a href="deletelink" class="delete">Delete</a>
    

    并将其放入<head>

    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script language="JavaScript" type="text/javascript">
    $(document).ready(function(){
        $("a.delete").click(function(e){
            if(!confirm('Are you sure?')){
                e.preventDefault();
                return false;
            }
            return true;
        });
    });
    </script>
    

回答by Zack Newsham

You have 2 options

你有2个选择

1) Use javascript to confirm deletion (use onsubmit event handler), however if the client has JS disabled, you're in trouble.

1)使用javascript确认删除(使用onsubmit事件处理程序),但是如果客户端禁用了JS,你就有麻烦了。

2) Use PHP to echo out a confirmation message, along with the contents of the form (hidden if you like) as well as a submit button called "confirmation", in PHP check if $_POST["confirmation"]is set.

2) 使用 PHP 回显确认消息,连同表单的内容(如果您愿意,可以隐藏)以及名为“确认”的提交按钮,在 PHP 中检查是否$_POST["confirmation"]设置。

回答by LOKESH

Call this function onclick of button

单击按钮时调用此函数

/*pass whatever you want instead of id */
function doConfirm(id) {
    var ok = confirm("Are you sure to Delete?");
    if (ok) {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                window.location = "create_dealer.php";
            }
        }

        xmlhttp.open("GET", "delete_dealer.php?id=" + id);
        // file name where delete code is written
        xmlhttp.send();
    }
}

回答by vikram

<?php 
$con = mysqli_connect("localhost","root","root","EmpDB") or die(mysqli_error($con));
 if(isset($_POST[add]))
 {
 $sno = mysqli_real_escape_string($con,$_POST[sno]);
   $name = mysqli_real_escape_string($con,$_POST[sname]);
   $course = mysqli_real_escape_string($con,$_POST[course]);
 
   $query = "insert into students(sno,name,course) values($sno,'$name','$course')";
   //echo $query;
   $result = mysqli_query($con,$query);
   printf ("New Record has id %d.\n", mysqli_insert_id($con));
  mysqli_close($con);
   
 }  
?>
  
<html>
<head>
<title>mysql_insert_id Example</title>
</head>
<body>
<form action="" method="POST">
Enter S.NO: <input type="text" name="sno"/><br/>
Enter Student Name: <input type="text" name="sname"/><br/>
Enter Course: <input type="text" name="course"/><br/>
<input type="submit" name="add" value="Add Student"/>
</form>
</body>
</html>

回答by Azuken

You can do it with an confirm()message using Javascript.

您可以confirm()通过使用 Javascript的消息来完成。

回答by Charaf JRA

Try this one :

试试这个:

 <script type="text/javascript">
      var baseUrl='http://example.com';
      function ConfirmDelete()
      {
            if (confirm("Delete Account?"))
                 location.href=baseUrl+'/deleteRecord.php';
      }
  </script>


  echo '<a type="button" onclick="ConfirmDelete()">DELETE ACCOUNT</a>';