我想要显示警报,以确保用户双重确认是否要删除数据。
<a onclick="return confirm('Are you sure?')" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
我的PHP代码:
$masId = $_GET['masId'];
$res=$MySQLiconn->query("Delete FROM masinflation WHERE masId=".$masId);
但是,即使点击取消,数据仍会被删除。
答案 0 :(得分:1)
如果单击取消按钮,则可以使用以下代码来防止锚标记的默认行为。
<a class="confirmation" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
<script type="text/javascript">
var elems = document.getElementsByClassName('confirmation');
var confirmIt = function (e) {
if (!confirm('Are you sure?')) e.preventDefault();
};
for (var i = 0, l = elems.length; i < l; i++) {
elems[i].addEventListener('click', confirmIt, false);
}
</script>
答案 1 :(得分:-1)
<a class="btn btn-large btn-danger" onclick="return checkDelete()" href="del.php?masId=<?php echo $row['masId'] ?>" >delete</a>
<script language="JavaScript" type="text/javascript">
function checkDelete(){
return confirm('Are you sure?');
}
</script>