表单提交后重定向回页面并显示模态

时间:2022-10-24 14:57:57

I'll try to explain this as best as possible.

我会尽力解释这个问题。

I have a form that on submit actions on email.php:

我有一个表单,在email.php上提交操作:

<form data-toggle="validator" role="form" method="POST" action="email.php">
                        <div class="row">
                            <!-- Full Name -->
                            <div class="form-group col-lg-4">
                                <label for="inputName" class="control-label">Name</label>
                                <input type="text" class="form-control" name="inputName" id="inputName" required>
                            </div>
                            <!-- email -->
                            <div class="form-group col-lg-4">
                              <label for="inputEmail" class="control-label">Email</label>
                              <input type="email" class="form-control" name="inputEmail" id="inputEmail" data-error="Incorrect Email Format" required>

                            </div>
                            <!-- phone number -->
                            <div class="form-group col-lg-4">
                                <label for="inputTel" class="control-label">Phone Number</label>
                                <input type="text" pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" name="inputTel" id="inputTel" data-minlength="10" maxlength="15" class="form-control" placeholder="123-456-7890" data-error="(123)-456-7890 | 1234567890 | 123-456-7890"> 

                            </div>
                            <!-- message -->
                            <div class="form-group col-lg-12">
                                <label for="content" class="control-label">Message</label>
                                <textarea name="content" id="content" class="form-control" rows="6" data-minlength="5" data-error="Message Must be longer"></textarea>

                            </div>
                            <!-- button -->
                            <div class="form-group col-lg-12">
                               <button type="submit" class="btn btn-default">Submit</button>
                            </div>
                        </div>
                    </form>

After the form is successfully processed in email.php, I want it to go back to the page the form is located on and show the "thank you" modal I created

在email.php中成功处理表单后,我希望它返回到表单所在的页面并显示我创建的“谢谢”模式

currently I have in email.php (after validation):

目前我在email.php(验证后):

     echo "<script>

     $(window).load(function(){
         $('#myModal').modal('show');
     });
     window.location.href="Websitename";
</script>";

This does not work. What happens is the page just gets redirected but does not display the modal.

这不起作用。发生的事情是页面被重定向但不显示模态。

Can anyone please provide me any assistance?

有人可以请我帮忙吗?

1 个解决方案

#1


0  

I think the problem is in

我认为问题在于

        window.location.href="Websitename";

When browser see this line , she will automatically locate the browser which means new request immediately made. Let me explain : Your form send request to email.php -----> sends response as a javascript --> javascript request to "website name". In order to correct this issue , you should put window.location.href inside timeout function and that timeout triggered after window.load . Please check :

当浏览器看到此行时,她将自动找到浏览器,这意味着立即发出新请求。让我解释一下:你的表单发送请求到email.php ----->将响应作为javascript - > javascript请求发送到“网站名称”。为了解决这个问题,你应该把window.location.href放在timeout函数中,并在window.load之后触发超时。请检查 :

    echo "<script>
          function deferredFunction(){
 window.location.href="Websitename";
    }
         $(window).load(function(){
             $('#myModal').modal('show');
             setTimeout(deferredFunction,5000);
         });   
    </script>";

Hope Helps!!

#1


0  

I think the problem is in

我认为问题在于

        window.location.href="Websitename";

When browser see this line , she will automatically locate the browser which means new request immediately made. Let me explain : Your form send request to email.php -----> sends response as a javascript --> javascript request to "website name". In order to correct this issue , you should put window.location.href inside timeout function and that timeout triggered after window.load . Please check :

当浏览器看到此行时,她将自动找到浏览器,这意味着立即发出新请求。让我解释一下:你的表单发送请求到email.php ----->将响应作为javascript - > javascript请求发送到“网站名称”。为了解决这个问题,你应该把window.location.href放在timeout函数中,并在window.load之后触发超时。请检查 :

    echo "<script>
          function deferredFunction(){
 window.location.href="Websitename";
    }
         $(window).load(function(){
             $('#myModal').modal('show');
             setTimeout(deferredFunction,5000);
         });   
    </script>";

Hope Helps!!