當在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'>×</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'>×</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 });
});
'$頭( '位置:nextpage.html');'刪除美元符號。 –
我意識到這篇文章非常類似於http://stackoverflow.com/questions/17157685/php-redirect-to-another-page-after-form-submit –