我使用Sweet Alert这个非常简单的形式,由于某种原因无法使其发挥作用。当我点击链接时,它会触发警报,当确认警报时,它会重新加载页面,因为它会提交表单。
但由于某种原因$_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();
});
});
我正在测试使用位于index.php上的PHP代码正确提交表单:
<?php
if(isset($_POST['delete_alert'])) {
echo "form is submitted correctly";
} else {
echo "something is wrong";
}
?>
答案 0 :(得分:1)
我认为问题出在aClass(localStr);
HTML表单仅在单击按钮时才会发送提交document.forms["form"].submit();
。
在这种情况下,它不是。
所以我认为你应该使用name=value
代替click
,但在你的情况下它不会起作用,因为点击将被你的javascript函数捕获。
所以这就是使用submit
答案 1 :(得分:0)
也许是工作: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']);
}
});
});
答案 2 :(得分:0)
我的表格需要稍微改变一下。通过Javascript提交表单时,不会单击提交按钮。因此,&#39; delete_alert&#39;从未设定过。
删除提交按钮并添加输入字段解决了问题。
正确的表格:
<form id="form" action="index.php" method="POST">
<input type="hidden" name="delete_alert' value="delete" />
</form>
感谢Barmar指出这一点。