Sweetalert2不返回true/false

问题描述:

我正在尝试使用SweetAlert2。它工作,当我不想通过$ _POST发送一些数据。但是当我想通过$ _POST发送数据时,sweetalert只会出现在不到一秒的时间,然后消散,并且不管用户选择何种形式发送。Sweetalert2不返回true/false

我该如何修改我的代码,当我想要定期窗口确认,当用户点击“删除”时,表单将被发送,否则当他点击“取消”时,表单会不会发送?

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> 

而不是使用处理程序,你可以在表单中使用的处理程序,更好这样

给形式的id也改变你的submit按钮正常按钮因为我们会根据用户的选择手动提交(或不提交)

<form id='myfrm' action="#" method="post"> <!-- u missed quote here -->    
<label> 
    <span>Username</span> 
    <input type="text" value="" name="username" /> 
</label> 
<!-- EDIT : removed (not working) 
<input type="button" name="submit" value="Register!" id="test-1" />  
--> 
<input type="hidden" name="submit" value="submit"/> 
</label>  
</form> 
<!-- EDIT : added --> 
<button name="submit" 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() { 

    // swal('Ready?!', 'Your form is going to be submitted.' 'success'); 
    // if you want to show alert that form is going to submit you may un-comment above line 
    document.querySelector('#myfrm').submit(); // manully submit 
    }) 
+0

谢谢,新手!但是它仍然不起作用(或者仅仅从部分 - 它成功地询问用户,如果他想删除它;但.then(function()不起作用)的第二部分:http://avcr1.chudst。 cz/test.php – chudst

+0

您可能会使用旧版本的sa2或sa1,它们在选项中使用回调函数,因此基于promise(then())语法在这些版本中不可用请参见[here](https:// jsfiddle.net/mmw6azgh/)它应该如何工作在最新版本 – Viney

+0

我认为,我使用的是最新版本(6.2.0)。而且你是正确的,“警报”是可以的,所以问题不是在“.then(function()”中,但直接在“document.querySelector('#myfrm')。submit();” - 表单仍然没有发送数据。 – chudst