2016-11-08 64 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

好的,空白頁面不再出現:)只是模態也不會出現。看起來像提交後刷新頁面,電子郵件仍然通過。 –