我有这个非常简单的表格,使用甜蜜警报,但由于某种原因无法让它工作。当我单击链接时,它会触发警报,当警报被确认时,它会重新加载页面,因为它提交了表单。
但出于某种原因,当$_POST['delete_alert']
未定义时。
触发警报的代码:
<a id="sa-warning">Delete</a>
我的表格:
<form id="form" action="index.php" method="POST">
<button type="submit" name="delete_alert" value="delete">Delete</button>
</form>
还有我的Javascript:
$('#sa-warning').click(function() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this alert.",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function() {
document.forms["form"].submit();
});
});
我正在测试表单是否已使用位于索引.php上的PHP代码正确提交:
<?php
if(isset($_POST['delete_alert'])) {
echo "form is submitted correctly";
} else {
echo "something is wrong";
}
?>
我认为问题出在document.forms["form"].submit();
HTML 表单仅在单击按钮时发送提交name=value
。在这种情况下,它不是。
所以我认为你应该使用 click
而不是 submit
,但在你的情况下它不起作用,因为点击会被你的 javascript 函数捕获。
所以这就是为什么最好的解决方案 si 使用input
也许是工作:http://rachmanzz.github.io/AlertlightJS
$('#sa-warning').click(function(){
alertlightJS.$swalPost({ // $swalGet for GET Method
title : "Are you sure?",
text : "You will not be able to recover this alert.",
type : "warning",
confirmText:"Yes, delete it!",
cancelText:"No!"
},{
url : "index.php",
input : {
delete_alert:'delete'
}
},function(isConfirm,data,swal){
if(isConfirm){
swal(['receive',data['data']]);
}else{
swal(['cancel','thanks']);
}
});
});
只需要稍微改变一下我的形式。通过 Javascript 提交表单时,不会单击提交按钮。因此,永远不会设置"delete_alert"的值。
删除提交按钮并添加输入字段解决了问题。
正确的形式:
<form id="form" action="index.php" method="POST">
<input type="hidden" name="delete_alert' value="delete" />
</form>
感谢巴马尔指出这一点。