简体   繁体   English

Sweet alert 2 在表单提交前显示

[英]Sweet alert 2 display before form submission

I have a code for a register account.我有一个注册帐户的代码。 What I want to happen is that the success alert displays before the forms submit and the page reloads or the success alert pops up after the form is successfully submitted.我想要发生的是在 forms 提交之前显示成功警报并且页面重新加载或在成功提交表单后弹出成功警报。

I tried this code and I it just submits the form and reload the page but the alert does not appear.我试过这段代码,我只是提交表单并重新加载页面,但没有出现警报。

` `


 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>";
  }

` `

I also tried another code, the alert works but the form is submitting twice我还尝试了另一个代码,警报有效但表单提交了两次

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>";
                  }

You are missing the function right after firing the sweet alert information.触发甜蜜警报信息后,您马上就错过了 function。 You can use in the following way:您可以通过以下方式使用:

$(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;
        }
    });
});

You can pass the URL directly or keep in the input field by setting a hidden attribute.您可以直接传递 URL 或通过设置隐藏属性保留在输入字段中。

I hope this helps and this is what you are looking for.我希望这会有所帮助,这就是您要找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM