2

私はSweetAlert2で作業しようとしています。$_POST を介してデータを送信したくない場合に機能します。しかし、$_POST を介してデータを送信したい場合、sweetalert は 1 秒未満で表示され、その後消え、ユーザーの選択に関係なくフォームが送信されます。

通常のウィンドウ確認が必要で、ユーザーが「削除」をクリックするとフォームが送信され、それ以外の場合は「キャンセル」をクリックするとフォームが送信されない場合、コードを変更するにはどうすればよいですか?

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>
4

1 に答える 1