我有一个表格,其视图是这样的
ID NAME LOCATION DELETE
1 sam US delete
我从表中删除了删除给定行的语句
echo "<td><a href=\"delete_members.php?id=".$row['id']."\">Delete</a></td>";
它重定向到delete_members.php页面并删除该行,但是我希望显示一个警告框,确保用户想要删除该行。因为它我有一个代码
<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>
delete_members.php页面的代码
<?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");
?>
但是,我无法以正确的方式使用声明中的确认脚本。我希望在我点击上面给出的第一个语句中出现的编辑按钮后弹出警报框(即脚本运行),然后如果用户确认它应该运行delete_members.php脚本。如果有人可以指导我,我将不胜感激
答案 0 :(得分:3)
以最简单的方式:
echo "<a onclick=\"return confirm('Delete this record?')\" href=\"delete_members.php?id=".$row['id']."\">delete</a>";
你也可以不引人注意的方式做到这一点,方法是在链接中添加一个类,然后使用类似jQuery的东西一次选择它们,然后将确认逻辑绑定到onclick事件。像这样:
$('a.delete').on('click', function() {
var choice = confirm('Do you really want to delete this record?');
if(choice === true) {
return true;
}
return false;
});
答案 1 :(得分:0)
echo "<a href='delete_members.php?id={$row['id']}' onclick=\"return confirm('Do you really want to delete this?')\">DELETE</a>";
答案 2 :(得分:0)
你可以试试这个。希望它对你有用..
为锚标记添加'删除'类。
echo "<td><a href=\"delete_members.php?id=".$row['id']."\" class=\"delete\">Delete</a></td>";
然后绑定删除功能:
$('.delete').on('click', myFunction);
答案 3 :(得分:0)
我认为这会有用,请看一下
$ ID = $行[ 'ID'];
如果你想传递id你可以使用$ id或者只是调用没有参数的函数
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>