简体   繁体   中英

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.

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

You can use vanillaJS Framework for this:

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

It's simple. Add the following code to your 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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