Reputation:
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
<button onclick="myFunction()">Delete</button>
<script>
function myFunction() {
var x;
if (confirm("Press a button!") == true) {
window.location="yourphppage.php"; // not sure which link should be placed here
return true;
} else {
window.location="index.php";
return true;
}
document.getElementById("demo").innerHTML = x;
}
</script>
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(i.e the script runs) 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
Upvotes: 0
Views: 27267
Reputation: 1064
I think this will be useful please look at this
$id=$row['id'];
If you want to pass id you can use $id or else just call function without params
echo "<a href='#' onclick='deletemember($id)'>Delete</a>";
<script>
function deletemember(id)
{
var r = confirm("Are you sure you want to delete");
if(r == true)
{
$.ajax({
type:"GET",
url:"delete_members.php",
data: ({id:id}),
success: function(data)
{
window.location="yourphppage.php";
}
});
}
else
{
window.location="index.php";
}
}
</script>
Upvotes: 0
Reputation: 752
You can try this. Hope it will work for you..
Add a 'delete' class to anchor tag.
echo "<td><a href=\"delete_members.php?id=".$row['id']."\" class=\"delete\">Delete</a></td>";
Then bind delete function:
$('.delete').on('click', myFunction);
Upvotes: 0
Reputation: 703
echo "<a href='delete_members.php?id={$row['id']}' onclick=\"return confirm('Do you really want to delete this?')\">DELETE</a>";
Upvotes: 0
Reputation: 6615
In the most simple way:
echo "<a onclick=\"return confirm('Delete this record?')\" href=\"delete_members.php?id=".$row['id']."\">delete</a>";
you could also do this in an unobtrusive manner, by adding a class to the links and selecting them all at once using something like jQuery and then bind the confirmation logic to the onclick event. Something like this:
$('a.delete').on('click', function() {
var choice = confirm('Do you really want to delete this record?');
if(choice === true) {
return true;
}
return false;
});
Upvotes: 5