在删除表格行之前显示一个确认框
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
我有一个表格,其视图是这样的 p>
ID NAME LOCATION DELETE
1 sam US delete
code> pre>
我从表中删除了给定行的语句 p>
echo“&lt; td&gt;&lt; a href = \”delete_members.php?id =“。$ row ['id']。”\“&gt;删除&lt; / a&gt;&lt; / td&gt;”;
code> pre>
它重定向到delete_members.php页面并删除该行,但是我希望显示一个警告框,确保用户想要删除该行。 我有一个代码 p>
&lt; button onclick =“myFunction()”&gt;删除&lt; / button&gt;
&lt; script&gt;
function myFunction(){
var x;
if(confirm(“按一个按钮!”)== true){
window.location =“yourphppage.php”; //不确定应该在哪里放置链接
返回true;
} else {
window.location =“index.php”;
返回true;
}
document.getElementById(“demo”) .innerHTML = x;
}
&lt; / script&gt;
code> pre>
delete_members.php页面的代码 p>
&lt;?php
include('admin_session.php');
$ con = mysqli_connect(“abc.com”,“abc”,“abc”,“abc”);
//检查连接
if (mysqli_connect_errno()){
echo“无法连接到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”);
?&gt;
code> pre>
但是,我无法以正确的方式将确认脚本与声明一起使用。 我希望在我点击上面给出的第一个语句中出现的编辑按钮后弹出警报框(即脚本运行),然后如果用户确认它应该运行delete_members.php脚本。 如果有人能指导我 p>
div>,我将不胜感激
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;
});
echo "<a href='delete_members.php?id={$row['id']}' onclick=\"return confirm('Do you really want to delete this?')\">DELETE</a>";
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);
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>