下面是我使用php的删除代码,我想在使用php的用户点击删除链接后得到确认。
<?php
include('conn.php');
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[st_id]'");
if($query)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Succesfully deleted')
window.location.href='mark-details1.php';
</SCRIPT>");
}
else
{
echo "Check your Server";
}
?>
请任何人都可以告诉我该怎么做?提前感谢!
你可以在HTML 标签上使用此 JavaScript 事件。
onclick="return confirm('you sure?');"
你也可以使用这个:
如果你的链接会发送一个像"?delete=(id("这样的获取
<?php
include('conn.php');
if(isset($_GET['delete']) && is_numeric($_GET['delete'])==1){
echo (a page with a form with confirmation question content that will sent a get for example (?checked_delete=(id)));
}elseif(isset($_GET["checked_delete"]) && is_numeric($_GET["checked_delete"])==1){
// TODO : deleting record.
$query=mysql_query("DELETE FROM mark WHERE student_id='$_GET[checked_delete]'");
header("location:mark-details1.php")
}else{
echo (normal page);
}
?>
我建议使用Ajax。在这里,我将使用 jQuery 来做到这一点
<a class="wh" data-id="<?=$rows['student_id']?>" href="edit-mark.php?st_id=<?=$rows['student_id']?>" title="Edit">Edit Marks</a><span class="confirmation"></span>
用
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
$.get(this.href,function(data) { // does the student still exist?
if (confirm("delete" +data+"?")) {
$.get("otherphp.php?st_id="+$(this).data("id"),function(data) {
$(this).next().html(data); // show response
});
}
});
});
});
或者为了隐藏蜘蛛的 href
<a class="wh" href="onlyworkswithjavascript.html"
data-id="<?=$rows['student_id']?>" data-href="edit-mark.php?st_id=<?=$rows['student_id']?>"
title="Delete">Delete Marks</a><span class="confirmation"></span>
$(function() {
$(".wh").on("click",function(e) {
e.preventDefault(); // cancel the click
if (confirm("delete" +data+"?")) {
$.get($(this).data("href"),function(data) {
$(this).next().html(data); // show response
});
}
});
});