Is it better to redirect to a 'thank you' page when a form is submitted in the PHP or JS. I am not concerned about any text being displayed on the page before the redirect.
I am probably providing too much code but I will provide the PHI and JS below for reference.
Will the below header work as expected after my BCC header?
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 = 'lindsay@domain.com'; // 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: lindsay@domain.com\r\n";
$headers .= "Reply-To: $email2\r\n";
$headers .= "Bcc: dan@domain.io\r\n";
mail($to,$email_subject,$email_body,$headers); // Post message
return true; ?>
JS
$(function()
{
var successMsg = "Your message has been sent."; // Message shown on success.
var failMsg = "Sorry it seems that our mail server is not responding, Sorry for the inconvenience!"; // Message shown on fail.
$("input,textarea").jqBootstrapValidation(
{
preventSubmit: true,
submitSuccess: function($form, event)
{
event.preventDefault(); // prevent default submit behaviour
var processorFile = "./bin/"+$form.attr('id')+".php";
var formData = {};
$form.find("input, textarea").each(function(e) // Loop over form objects build data object
{
formData[$(this).attr('id')] = $(this).val();
});
$.ajax({
url: processorFile,
type: "POST",
data: formData,
cache: false,
success: function() // Success
{
$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>");
},
error: function() // Fail
{
$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>");
},
complete: function() // Clear
{
$form.trigger("reset");
},
});
},
filter: function() // Handle hidden form elements
{
return $(this).is(":visible");
},
});
});