我正在尝试使用 SweetAlert2。当我不想通过 $_POST 发送一些数据时,它可以工作。但是当我想通过 $_POST 发送数据时,sweetalert 只会出现不到一秒钟,然后消失,无论用户选择如何发送表单。
如何修改我的代码,当我想要定期窗口确认并且当用户点击“删除”时,表单将被发送,否则当他点击“取消”时,表单将不会发送?
if(isset($_POST['submit'])) {
echo 'Your form was submitted.';
}
<form action="#" method="post>
<label>
<span>Username</span>
<input type="text" value="" name="username" />
</label>
<input type="submit" name="submit" value="Register!" id="test-1" />
</label>
</form>
<script>
document.querySelector('#test-1').onclick = function(){
swal({
title: 'Are you sure?',
text: "You won't be able to revert this!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then(function () {
swal(
'Deleted!',
'Your file has been deleted.',
'success'
)
})
//IF SWAL => DELETE -> send form
//ELSE IF SWAL => CANCEL -> do nothing
//when I put here "return false;", then sweetalert is working, but it doesn't send the form
</script>
感谢新手的解决方案:
<?php if(isset($_POST['send']))
{echo 'Your form was submitted.';} ?>
<form id='myfrm' action="#" method="post">
<label>
<span>Username</span>
<input type="text" value="" name="username" />
<input type="hidden" name="send" value="send">
</label>
</form>
<button value="Register!" id="test-1"> submit </button>
<script>
document.querySelector("#test-1").onclick = function(){
swal({
title: "Are you sure?",
text: "You won't be able to revert this!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then(function () {
document.querySelector("#myfrm").submit();
})
}
</script>