0

I have a form I got working fine but when I try and redirect after a form submission to a thank you page but get and error of " Cannot modify header information - headers already sent by ". I understand the headers are being modified in the mail() part but how do you do this without an error?

Code:

<?php
//if "email" variable is filled out, send email
  if ( isset($_REQUEST['email']) ) {

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you
       submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

   // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }

  //Form Variables
    //Personal Information 
    $subject = "Smile Guide";
    $email = $_REQUEST['email'];
    $first_name = $_REQUEST['first_name'];
    $last_name = $_REQUEST['last_name'];
    $phone = $_REQUEST['phone'];
    $admin_email = "packy@ocularlogic.com,$email";

    //Stage 1
    $chipped = $_REQUEST['smile-fault-1'];
    $color = $_REQUEST['smile-fault-2'];
    $gaps = $_REQUEST['smile-fault-3'];
    $worn = $_REQUEST['smile-fault-4'];
    $crooked = $_REQUEST['smile-fault-5'];
    $metal = $_REQUEST['smile-fault-6'];

    //Stage 2
    $aggressive = $_REQUEST['smile-prefered-1'];
    $mature = $_REQUEST['smile-prefered-2'];
    $vigorous = $_REQUEST['smile-prefered-3'];
    $dominant = $_REQUEST['smile-prefered-4'];
    $focused = $_REQUEST['smile-prefered-5'];
    $enhanced = $_REQUEST['smile-prefered-6'];
    $soften = $_REQUEST['smile-prefered-7'];
    $hollywood = $_REQUEST['smile-prefered-8'];
    $functional = $_REQUEST['smile-prefered-9'];
    $natural = $_REQUEST['smile-prefered-10'];
    $youthful = $_REQUEST['smile-prefered-11'];
    $oval = $_REQUEST['smile-prefered-12'];

    //Stage 3
    $shade = $_REQUEST['smile-color'];  

    //List errors
     $error_message = "";
        $string_exp = "/^[A-Za-z0-9 .'-]+$/";
      if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
      if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }  
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
      if(!preg_match($string_exp,$phone)) {
        $error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }      

    //The Message
    $content = '<html><body><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center">';
    $content .= '<img src="http://smiletothemaxdentallab.com/guide/smile-logo.jpg" style="display: block; margin: 10px auto;">';
    $content .= '<h1 style="color: #c51f4f;">Smile Guide</h1>';
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Client Information</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= "<li>Name:".$last_name.",".$first_name."</li>";
    $content .= "<li>Phone:".$phone."</li>";
    $content .= "<li>Email:".$email."</li>";
    $content .= "</ul>";
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Current Teeth Issues</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= isset($_POST['smile-fault-1']) ? "<li>".$chipped."</li>" : "";
    $content .= isset($_POST['smile-fault-2']) ? "<li>".$color."</li>" : "";
    $content .= isset($_POST['smile-fault-3']) ? "<li>".$gaps."</li>" : "";
    $content .= isset($_POST['smile-fault-4']) ? "<li>".$worn."</li>" : "";
    $content .= isset($_POST['smile-fault-5']) ? "<li>".$crooked."</li>" : "";
    $content .= isset($_POST['smile-fault-6']) ? "<li>".$metal."</li>" : "";
    $content .= "</ul>";
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Desired Color</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= "<li>".$shade."</li>";
    $content .= "</ul>";
    $content .= "</td></tr><tr><td style='background-color:#F7F7F7; color:#999999; text-align:center; padding: 50px 0;'><p>Brought to you be AOC Dental Lab</p><p>Toll free (800) 729-1593</p><p>1724 E Sherman Ave | Coeur d’Alene, Idaho | 83814</p>";
    $content .= "</td></tr></table></body></html>";

    //Set up Header
    $headers = "From: " . strip_tags($_POST['email']) . "\r\n";
    $headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    //Send Email
    $send = mail($admin_email, $subject, $content, $headers);

    if($send) {
        header("Location: http://website.com/smile-guide/thank-you");
    } 

}

?>
Packy
  • 3,405
  • 9
  • 50
  • 87
  • Place an `exit()` instead of redirection and render your page. Open source code with `Ctrl+U` and ensure you don't have any spaces or other symbols as body before or in the middle of your html – Croll Nov 11 '15 at 22:01

2 Answers2

0

This error is being caused by the fact that you have already sent data to the client at the moment you are trying to send the headers (which should be sent first).

Check your page encoding. Probably it is UTF-8+ (with UTF-8 signature)

If not then you need to check white spaces.

Croll
  • 3,631
  • 6
  • 30
  • 63
0

Don't forget to exit after redirect

if($send) {
  header("Location: http://website.com/smile-guide/thank-you");
  exit();
} 
meda
  • 45,103
  • 14
  • 92
  • 122