簡體   English   中英

Sweet alert 2 在表單提交前顯示

[英]Sweet alert 2 display before form submission

我有一個注冊帳戶的代碼。 我想要發生的是在 forms 提交之前顯示成功警報並且頁面重新加載或在成功提交表單后彈出成功警報。

我試過這段代碼,我只是提交表單並重新加載頁面,但沒有出現警報。

`


 if ($password == $cpassword) {
   $query1 = mysqli_query($connections, $query);
     if ($query1){
      echo "
           <script type='text/javascript'>setTimeout(function () { 
                Swal.fire({
                  title: 'Success!',
                  icon: 'success',
                  text: 'Account registered!',
                  allowOutsideClick: true,
                  allowEscapeKey: true,
                  allowEnterKey: false,
                  showConfirmButton: false,
                  }); 
                },100) </script>";
              }
              
        echo "<script>window.location.href='accounts.php'</script>";
  }

`

我還嘗試了另一個代碼,警報有效但表單提交了兩次

if ($password == $cpassword) {
              mysqli_query($connections, $query);
              echo "
              <script type='text/javascript'>setTimeout(function () { 
                Swal.fire({
                  title: 'Success!',
                  icon: 'success',
                  text: 'Account registered!',
                  allowOutsideClick: true,
                  allowEscapeKey: true,
                  allowEnterKey: false,
                  showConfirmButton: false,
                  }); 
                  },100)

                  window.setTimeout(function(){ 
                    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
                    } ,100);
                    </script>";
                  }

觸發甜蜜警報信息后,您馬上就錯過了 function。 您可以通過以下方式使用:

$(document).on('click', '.delete_trigger_btn', function () {
  let redirect_url = $(this).attr('data-href');
    Swal.fire({
        title: 'Success!',
        icon: 'success',
        text: 'Account registered!',
        allowOutsideClick: true,
        allowEscapeKey: true,
        allowEnterKey: false,
        showConfirmButton: false,
    }).then(function (t) {
        if (t.value == true) {
            window.location.href = redirect_url;
        }
    });
});

您可以直接傳遞 URL 或通過設置隱藏屬性保留在輸入字段中。

我希望這會有所幫助,這就是您要找的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM