2016-11-08 151 views
1

对这个问题寻找几个小时的答案,但我完全堆积,大脑冻结。提交表格后无法摆脱空白的PHP页面

已成功提交订阅php窗体与引导模式。一切正常,电子邮件通过,模态显示只有一秒钟或更少的空白页面出现。

我猜这是加载之前form.php文件是一个单独的文件,但它有一种方法来停止加载空白页?

这里是HTML代码

<form action="form.php" method="post"> 
 
    <div class="form-group label-floating"> 
 
    <input name="email" class="control-label form-control text-center" type="text" placeholder="Enter your email address ..."> 
 
    <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Send</button> 
 
    </div> 
 
</form> \t 
 

 

 
<!-- Sart Modal --> 
 
\t <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
 
\t \t <div class="modal-dialog"> 
 
\t \t \t <div class="modal-content"> 
 
\t \t \t \t <div class="modal-header"> 
 
\t \t \t \t \t <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> 
 
\t \t \t \t \t \t <i class="material-icons">clear</i> 
 
\t \t \t \t \t </button> 
 
\t \t \t \t \t <h4 class="modal-title">Thank you</h4> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="modal-body"> 
 
\t \t \t \t \t <p>Thank you for registering, we have added you to the waiting list! 
 
\t \t \t \t \t </p> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="modal-footer"> 
 
\t \t \t \t \t <button type="button" class="btn btn-danger btn-simple" data-dismiss="modal">Close</button> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t </div> 
 
<!-- End Modal -->

这里是PHP代码

<?php 
 
$to = "[email protected]"; 
 
$from = "[email protected]"; 
 

 
$headers = "From: " . $from . "\r\n"; 
 

 
$subject = "New Beta Subscription"; 
 
$body = "New user interested in beta program: " . $_POST['email']; 
 

 

 
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) 
 
{ 
 
    if (mail($to, $subject, $body, $headers, "-f " . $from)) 
 
    { 
 
     echo "<script type='text/javascript'> 
 
       $(document).ready(function(){ 
 
       $('#myModal').modal('show'); 
 
       window.stop(); 
 
       }); 
 
       </script>"; 
 
    } 
 
    else 
 
    { 
 
     echo 'There was a problem with your e-mail (' . $_POST['email'] . ')'; 
 
    } 
 
} 
 
else 
 
{ 
 
    echo 'There was a problem with your e-mail (' . $_POST['email'] . ')'; 
 
}

而且要改变错误消息SH也超过了模态,它产生了不知道是否可以从同一索引文件调用两个模态?

任何帮助非常欢迎! 谢谢,K>

+1

我认为使用php'json_encode'并在前端使用'Ajax'会更好。 – claudios

回答

1

你试图实现什么似乎不同于你实际编码。

让我们来看看你的HTML表单。您在提交按钮上附加了Bootstrap的data-toggledata-target属性。这意味着当你点击该按钮时,它将打开模式并提交表单。因此,用户将简要地看到一个模式,并将该页面重定向到您的PHP文件。 (这就是为什么你看到一个模态出现简要。)

接下来,让我们看看你的PHP文件。首先,当你从一个页面提交表单到另一个页面时,后一页面不知道你以前的页面中的HTML元素。这意味着您在echo'd <script>标记中的代码实际上不应该工作,因为它正在寻找您以前的页面上的HTML元素。

现在,您的问题,为什么你得到一个空白页?那么......一切工作正常,所以你的代码回声是一个<script>标签 - 它没有可视指示器。但就像我刚刚说的,你在<script>里面有什么不起作用 - 所以什么也没有出现,什么也没有发生。

所以,当你点击你的按钮时,事件顺序的重述:模态显示,表单提交,表单重定向到另一个页面,而其他页面回显没有任何内容。

下面是一个贫穷的/快速的解决方案是什么,我认为你正在努力实现:

  1. 更改HTML文件到PHP文件。

  2. 删除data-toggledata-target属性关闭您的按钮,这样,当你点击按钮

    <form action="form.php" method="post"> 
        <div class="form-group label-floating"> 
         <input name="email" class="control-label form-control text-center" type="text" placeholder="Enter your email address ..."> 
         <button type="submit" class="btn btn-primary">Send</button> 
        </div> 
    </form> 
    
  3. 从PHP页面提交您的移动回显script标签不打开模态权你的PHP表单页面,敷在条件如下图所示:

    <?php if (!empty($_GET['success'])) : ?> 
        <script> 
         $(document).ready(function(){ 
          $("#myModal").modal(); 
         }); 
        </script> 
    <?php endif ?> 
    
  4. 在你的PHP SUBM删除的代码的回显script的标记线ission页面。相反,请添加代码,以便将其重定向回您的PHP表单页面。关键部分是您将在URL的末尾附加?success=true

    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); // valid email or null|false 
    
    if ($email) { 
        $to = "[email protected]"; 
        $from = "[email protected]"; 
        $headers = "From: " . $from . "\r\n"; 
        $subject = "New Beta Subscription"; 
        $body = "New user interested in beta program: " . $email; 
    
        if (mail($to, $subject, $body, $headers, "-f " . $from)) { 
         header('Location: subscribe.php?success=true'); // replace `subscribe.php` with PHP form page 
         exit; 
        } 
        echo 'There was a problem with your e-mail (' . $email . ')'; 
    } else { 
        echo 'There was a problem with your e-mail'; // no point in printing $email if it is null 
    } 
    

基本上,通过?success=true是告诉PHP表单页面,一切顺利打开模式(3)。

而且应该是这样。

更好的方法是学习和使用AJAX。

+0

感谢您的详细解释和一步一步的指导。由于某种原因,无法使其工作...现在我得到内部服务器错误,而不是该空白页面,但提交的电子邮件仍然通过... –

+0

它可能是'位置:'字符串,我忘了包括在'header()'函数中。 – Mikey

+0

好的,空白页面不再出现:)只是模态也不会出现。看起来像提交后刷新页面,电子邮件仍然通过。 –