2016-12-03 137 views
0

我正在嘗試使用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> 

回答

1

而不是使用處理程序,你可以在表單中使用的處理程序,更好這樣

給形式的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