0

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'>&times;</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'>&times;</button><strong>"+failMsg+"</strong></div></div>"); 
       },
    complete: function() // Clear
    {
     $form.trigger("reset");
    },
      });
         },
         filter: function() // Handle hidden form elements
   {
    return $(this).is(":visible");
         },
  });
});
Daniel Barton
  • 39
  • 1
  • 9

2 Answers2

1

Use window.location on success

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

Yes the header should redirect to nextpage.html which should be in the current directory. You should place an exit() call right after the header to make sure nothing else is outputted before the header is sent to the browser for redirection. So in the end you would end up with

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

If you don't place the exit call and let the script continue with code execution there is a chance that some code may generate output which will stop the redirect from happening.

The above code applies to PHP, for JS use window.location.href for redirection. This would look like this

<script>
window.location.href = 'nextpage.html'
</script>
Umair Shah
  • 2,305
  • 2
  • 25
  • 50