Reputation:
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.
Upvotes: 0
Views: 87
Reputation: 3006
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>
Upvotes: 0
Reputation: 5517
Use the confirm
js function:
<a href="delete.php?id=.." onclick="return confirm('are you sure?')">
Upvotes: 2
Reputation: 14982
You can use vanillaJS Framework for this:
if (confirm('Are you sure?')) {
alert('deleted');
} else {
alert('Cancelled');
}
Upvotes: 0
Reputation: 3494
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.
Upvotes: 0