2015-07-21 135 views
0

當在PHP或JS中提交表單時,重定向到「謝謝」頁面會更好嗎?我不擔心在重定向之前頁面上顯示的任何文本。在PHP或JS中提交表單後重定向到URL

我可能提供了太多的代碼,但我會提供PHI和JS以供參考。

下面的標題會在我的BCC標題後按預期工作嗎?

header('Location: nextpage.html'); 

PHP

<?php 
if(empty($_POST['name2']) || empty($_POST['email2']) || empty($_POST['message2'])) 
{ 
    return false; 
} 

$name2 = $_POST['name2']; 
$email2 = $_POST['email2']; 
$message2 = $_POST['message2']; 

$to = '[email protected]'; // Email submissions are sent to this email 

// Create email 
$email_subject = "Message from Domain 8.1"; 
$email_body = "You have received a new message. \n\n". 
       "Name2: $name2 \nEmail2: $email2 \nMessage2: $message2 \n"; 
$headers = "From: [email protected]\r\n"; 
$headers .= "Reply-To: $email2\r\n"; 
$headers .= "Bcc: [email protected]\r\n"; 

mail($to,$email_subject,$email_body,$headers); // Post message 
return true;   ?> 

JS

$(function() 
 
{ 
 
\t var successMsg = "Your message has been sent."; // Message shown on success. 
 
\t var failMsg = "Sorry it seems that our mail server is not responding, Sorry for the inconvenience!"; // Message shown on fail. 
 
\t 
 
\t $("input,textarea").jqBootstrapValidation(
 
    { 
 
     \t preventSubmit: true, 
 
     \t submitSuccess: function($form, event) 
 
\t \t { 
 
\t \t \t event.preventDefault(); // prevent default submit behaviour 
 
\t \t \t 
 
\t \t \t var processorFile = "./bin/"+$form.attr('id')+".php"; 
 
\t \t \t var formData = {}; 
 

 
\t \t \t $form.find("input, textarea").each(function(e) // Loop over form objects build data object 
 
\t \t \t { \t \t 
 
\t \t \t \t formData[$(this).attr('id')] = $(this).val(); \t 
 
\t \t \t }); 
 
\t 
 
\t \t \t $.ajax({ 
 
\t \t   url: processorFile, 
 
\t \t  \t type: "POST", 
 
\t \t  \t data: formData, 
 
\t \t  \t cache: false, 
 
\t \t  \t success: function() // Success 
 
\t \t \t \t { 
 
\t \t \t \t \t $form.append("<div id='form-alert'><div class='alert alert-success'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><strong>"+successMsg+"</strong></div></div>"); \t \t 
 
\t \t \t  \t }, 
 
\t \t \t  \t error: function() // Fail 
 
\t \t \t  \t { 
 
\t \t \t \t \t $form.append("<div id='form-alert'><div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><strong>"+failMsg+"</strong></div></div>"); \t 
 
\t \t \t  \t }, 
 
\t \t \t \t complete: function() // Clear 
 
\t \t \t \t { 
 
\t \t \t \t \t $form.trigger("reset"); 
 
\t \t \t \t }, 
 
\t \t  \t }); 
 
     }, 
 
     filter: function() // Handle hidden form elements 
 
\t \t { 
 
\t \t \t return $(this).is(":visible"); 
 
     }, 
 
\t }); 
 
});

+2

'$頭( '位置:nextpage.html');'刪除美元符號。 –

+0

我意識到這篇文章非常類似於http://stackoverflow.com/questions/17157685/php-redirect-to-another-page-after-form-submit –

回答

1

使用window.location的是頭應該重定向到nextpage.html這應該是在當前目錄。您應該在標題後立即調用exit(),以確保在將標題發送到瀏覽器進行重定向之前,不會輸出任何內容。那麼,到底你最終會與

<?php ... header('Location: nextpage.html'); exit();

如果不將退出呼叫並讓腳本代碼執行繼續有機會的話,一些代碼可以生成輸出,將停止重定向從發生。

上述代碼適用於PHP,JS使用window.location.href進行重定向。這應該是這樣的

<script> ... window.location.href = 'nextpage.html' </script>

1

成功

success: function() // Success 
{ 
    $form.append(""); 
    window.location = 'newURL.html'; 
}