简体   繁体   English

重定向前如何提醒用户

[英]How can I alert user, before redirect

I have a function where I get all information in my users. 我有一个可以在用户中获取所有信息的功能。 At the end of the string, I give the admin the option to delete the user, based on the users id. 在字符串的末尾,我给admin提供了基于用户ID删除用户的选项。

My string: 我的字符串:

while ( $row = $result->fetch_assoc () ) {
   $string .= "<tr><td>" . $row ['id'] . "</td><td>" . $row ['first_name'] . "</td><td>" . $row ['last_name'] . "</td><td>" . $row ['email'] . "</td><td>" . $row ['role_name'] . "</td><td>[<a href='delete.php?id=" . $row ['id'] . "'>Delete</a>]</td></tr>";
}

My delete page: 我的删除页面:

if ($user->deleteUser($_GET['id']))
{
    header("Location: admin.php");
}
else
{
    echo "Could not delete the user!";
}

And my delete user function: 和我的删除用户功能:

public function deleteUser($id)
{
    $sql = "DELETE FROM users WHERE id = ?";
    if (!$result = $this->db->mysqli->prepare($sql))
    {
        return false;
    }

    if (!$result->bind_param('i', $id))
    {
        return false;
    }

    return $result->execute();
}

And all of this works fine. 所有这些都很好。

What I wan't to know, is how can I promt admin with an alert, saying: "Are you sure?" 我不知道的是,我该如何警告管理员,说:“您确定吗?” for example. 例如。

使用confirm js函数:

<a href="delete.php?id=.." onclick="return confirm('are you sure?')">

You can use javascript on click with your links. 您可以在点击链接时使用javascript。 for example change 例如改变

<a href='delete.php?id=".$row['id']"'>Delete</a>

to

<a href='delete.php?id=".$row['id']."' onclick='return confirm(\'are you sure?\')'>Delete</a>

this will call a javascript confirm box before you're taken to the link. 在进入该链接之前,这将调用一个javascript确认框。

You can use vanillaJS Framework for this: 您可以为此使用vanillaJS Framework:

if (confirm('Are you sure?')) {
  alert('deleted');
} else {
  alert('Cancelled');
}

It's simple. 这很简单。 Add the following code to your javascript. 将以下代码添加到您的javascript中。

 <a href='delete.php?id=" . $row ['id'] . "'onclick="deletPost()">Delete</a>
//Javascript
<script>
function deletePost() {
    var ask = window.confirm("Are you sure you want to delete this post?");
    if (ask) {
        //Do something if approved.
    }
}</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM