title: Modify php mail script to display a popup message and redirect to another page on the website
body: <p>I have a form that when submitted sends an email using the inbuilt php <code>mail()</code> function.</p>
  
  <p>How do I either:</p>
  
  <ol>
  <li>Display a pop-up message to confirm the form was submitted, and the email sent.</li>
  </ol>
  
  <p><em>or</em></p>
  
  <ol start="2">
  <li>Redirect to another page once complete</li>
  </ol>
  
  <p>I would prefer option 1.</p>
  
  <pre><code>if($_POST) {   
      $name = trim(stripslashes($_POST['contactName']));
      $email = trim(stripslashes($_POST['contactEmail']));
      $subject = trim(stripslashes($_POST['contactSubject']));
      $contact_message = trim(stripslashes($_POST['contactMessage']));
  
      // Check Name
      if (strlen($name) &lt; 2) {
          $error['name'] = "Please enter your name.";
      }
      // Check Email
      if (!preg_match('/^[a-z0-9&amp;\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
          $error['email'] = "Please enter a valid email address.";
      }
      // Check Message
      if (strlen($contact_message) &lt; 15) {
          $error['message'] = "Please enter your message. It should have at least 15 characters.";
      }
      // Subject
      if ($subject == '') { $subject = "Contact Form Submission"; }
  
  
      // Set Message
      $message .= "Email from: " . $name . "&lt;br /&gt;";
      $message .= "Email address: " . $email . "&lt;br /&gt;";
      $message .= "Message: &lt;br /&gt;";
      $message .= $contact_message;
      $message .= "&lt;br /&gt; ----- &lt;br /&gt; This email was sent from your site's contact form. &lt;br /&gt;";
  
      // Set From: header
      $from =  $name . " &lt;" . $email . "&gt;";
  
      // Email Headers
      $headers = "From: " . $from . "\r\n";
      $headers .= "Reply-To: ". $email . "\r\n";
      $headers .= "MIME-Version: 1.0\r\n";
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
  
  
      if (!$error) {
          ini_set("sendmail_from", $siteOwnersEmail); // for windows server
          $mail = mail($siteOwnersEmail, $subject, $message, $headers);
          if ($mail) { echo "ok. (Here pop up saying its submitted and redirects)"; }
          else { echo "Something went wrong. Please try again."; }
      } # end if - no validation error
      else {
          $response = (isset($error['name'])) ? $error['name'] . "&lt;br /&gt; \n" : null;
          $response .= (isset($error['email'])) ? $error['email'] . "&lt;br /&gt; \n" : null;
          $response .= (isset($error['message'])) ? $error['message'] . "&lt;br /&gt;" : null;
          echo $response;
      } # end if - there was a validation error
  }
  
  ?&gt;
  </code></pre>