How can I make php redirect after email message was sent?

Sometimes, we all need a little general help.

Post Permalink
by Azrath » 6/19/2017

So I am trying to make redirection after submitting the form to the home page. Fine, it works BUT there is one problem. The message is not displayed as user presses "Submit" button but is instantly redirected to the Home page. How can I make it so users first recieve "Thank you" message and than are redirected to the Home page?

Code: Select all
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to = [email protected]';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
  <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'contact_page.html';
  </script>
  <?php
}
else { ?>
    <script language="javascript" type="text/javascript">
      alert('Message failed. Please, send an email to [email protected]');
      window.location = 'contact_page.html';
    </script>
    <?php
}
header('Location: https://saprs.000webhostapp.com/index.html');
?>
User avatar
Level 8
Apprentice Scribe
Posts: 1277
Joined: 6/28/15
Says: Nostalgia is a hard thing when it hits where it hurts the most.

Return to General Help