I have a table whose view is something like this

ID  NAME  LOCATION  DELETE
1   sam   US        delete

I have a statement from the table that deletes the given row

echo "<td><a href=\"delete_members.php?id=".$row['id']."\">Delete</a></td>";

It redirects to delete_members.php page and the row gets deleted, however i wish to display an alert box that makes sure that the user wants to delete the row or not. for it i have a code

code for the delete_members.php page

<?php
include('admin_session.php');
$con=mysqli_connect("abc.com","abc","abc","abc");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id']; 

mysqli_query($con,"DELETE FROM members WHERE id='".$id."'");
mysqli_close($con);
header("Location: admin_member_list.php");
?> 

However, i am unable to use the confirmation script with the statement in a proper way. i want that the alert box gets popped up after i click on the edit button that is present on the first statement given above and then if the user confirms it should run the delete_members.php script. would appreciate if someone could guide me

Dani AI

Generated

Good call from and : you want a confirm dialog (not alert) that cancels navigation when the user clicks No. If you are generating many rows, prefer unobtrusive JS with event delegation so you do not inline JS on every link and it also works for dynamically added rows.

HTML (per row):

<a class="js-delete" href="delete_members.php?id=1" data-label="sam">Delete</a>

JS (once, on the page):

document.addEventListener('click', function (e) {
  const link = e.target.closest('a.js-delete');
  if (!link) return;
  const label = link.dataset.label || 'this row';
  if (!confirm('Delete ' + label + '?')) {
    e.preventDefault(); // stay on the page
  }
});

A few important hardening tips while you are at it:

  • Avoid using GET for destructive actions. Prefer a small <form method="post"> with a hidden id, and intercept the form submit to show the same confirm() before allowing it to post.
  • Always validate and use prepared statements on the server. Example:
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if (!$id) { http_response_code(400); exit; }
$stmt = $con->prepare('DELETE FROM members WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
header('Location: admin_member_list.php');
exit;
  • Add a CSRF token to the form and verify it server-side.
  • After header('Location: ...'), call exit; to stop further output.

With this setup you get a clean UI, the dialog appears when the user clicks Delete, and your delete endpoint remains safe even if someone bypasses JavaScript.

Recommended Answers

All 2 Replies

Use the onclick of the a tag to popup a dialog. If it returns false processing stops. If it returns true your delete script will be called.

You can use the onclick attribute as pritaeas suggested, together with the JavaScript confirm() function to give a popup with Yes or No options:

echo "<td><a href=\"delete_members.php?id=".$row['id']."\" onclick="return confirm('Are you sure you wish to delete this item?');">Delete</a></td>";

If the user clicks Yes, the function returns true, and the link is followed. If the user clicks No, the function returns false, and nothing happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.