0

I have read several answers and questions, however, I still cannot seem to get my header() to work. This is just a simple contact form, and This is my last step to send guests to a thankyou page. What am I missing.

  <?php

    $fname = $lname = $cname = $email = $budget = $services = "";

    $error_counter = 0;

    $error_report = "";

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {


      if (empty($_POST['fname'])) {
        $fname_error = 'Please provide your first name.';
        $error_counter++;
      } else {
        $fname = test_input($_POST['fname']);

        if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
          $fnameErr = "Only letters and white space allowed";
          $error_counter++;
        }
      }
      if (empty($_POST['lname'])) {
        $lname_error = 'Please provide your last name.';
        $error_counter++;
      } else {
        $lname = test_input($_POST['lname']);

        if (!preg_match("/[a-zA-Z \.]/",$lname)) {
          $lnameErr = "Only letters and white space allowed";
          $error_counter++;
        }
      }
      if (empty($_POST['cname'])) {
        $cname = '';
      } else {
        $cname = test_input($_POST['cname']);

        if (!preg_match("/^[a-zA-Z0-9 \.]*$/",$cname)) {
          $cnameErr = "Only letters and white space allowed";
          $error_counter++;
        }
      }
      if (empty($_POST['phone'])) {
        $phone = '';
      } else {
        $phone = test_input($_POST['phone']);

        if (!preg_match("/^[()\-0-9 \.]*$/",$phone)) {
          $phoneErr = "Please use only the following: ( ) - . 0-9.";
          $error_counter++;
        }
      }
      if (empty($_POST['email'])) {
        $email_error = 'Please provide an email so that I can get back in touch with you.';
        $error_counter++;
      }  else {
        $email = test_input($_POST['email']);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { //validate email
          $emailErr = "Invalid email format";
          $error_counter++;
        }
      }
      if (empty($_POST['budget'])) {
        $budget_error = 'Please provide an estimated budget.';
        $error_counter++;
      } else {
        $budget = test_input($_POST['budget']);
      }
      if (empty($_POST['textarea'])) {
        $textarea = '';
      } else {
        $textarea = test_input($_POST['textarea']);
      }

      if (isset($_POST['new-website'])) {
        $services = $services."New Website<br>";
      }
      if (isset($_POST['website-redesign'])) {
        $services = $services."Website Re-design<br>";
      }
      if (isset($_POST['mobile-website'])) {
        $services = $services."Mobile Website<br>";
      }
      if (isset($_POST['online-resume'])) {
        $services = $services."Online Resume<br>";
      }
      if (isset($_POST['non-profit-website'])) {
        $services = $services."Non-profit Website<br>";
      }
      if (isset($_POST['seo'])) {
        $services = $services."SEO<br>";
      }
      if (isset($_POST['google-adwords'])) {
        $services = $services."Google AdWords<br>";
      }
      if (isset($_POST['graphics-design'])) {
        $services = $services."Graphics Design<br>";
      }
      if (isset($_POST['other'])) {
        $services = $services."Other<br>";
      }

      $fname = test_input($_POST['fname']);
      $lname = test_input($_POST['lname']);
      $cname = test_input($_POST['cname']);
      $phone = test_input($_POST['phone']);
      $email = test_input($_POST['email']);
      $budget = test_input($_POST['budget']);
      $textarea = test_input($_POST['textarea']);

      if ($error_counter == 0) {

        $to = "dpeaches96@gmail.com";
        $subject = "Website Contact Peachwebdev";

        $name_final = "Name: ".$fname." ".$lname."<br><br>";
        $company_final = "Company: ".$cname."<br><br>";
        $phone_final = "Phone Number: ".$phone."<br><br>";
        $email_final = "Email: ".$email."<br><br>";
        $budget_final = "Est. Budget: ".$budget."<br><br>";
        $services_final = "Services: <br>".$services."<br><br>";
        $textarea_final = "Comments:  ".$textarea."<br><br>";
        $message =  $name_final.$company_final.$phone_final.$email_final.$budget_final.$services_final.$textarea_final;


        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: '.$email;


        mail($to,$subject,$message,$headers);

        header("Location: http://www.peachwebdev.com/pages/thankyou.html");
        exit;

      } else {
        echo '<script type="text/javascript"> alert(\'There were errors in your form. Please try again.\'); </script>';
        $error_report = "<div class='alert alert-danger'>There were errors in your form, please correct and submit again.</div>";
      }

    }

    function test_input($data) {
      $data = htmlspecialchars($data);
      $data = trim($data);
      $data = stripslashes($data);
      return $data;
    }

     ?>

And I am aware that my code can probably made better, so if there are suggestions on simplifying or condensing, I would gladly appreciate it!

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • Have you checked your error logs? – Jay Blanchard Jun 08 '17 at 19:51
  • Do you get an error? – Remco K. Jun 08 '17 at 19:53
  • @JayBlanchard I get this error: Warning: Cannot modify header information - headers already sent by (output started at /home/sites/1b/a/a84868269e/public_html/pages/contact.php:25) in /home/sites/1b/a/a84868269e/public_html/pages/contact.php on line 162. But it seams that means that it is finding the one that i want it to? I dont quite understand this. –  Jun 08 '17 at 19:56

1 Answers1

0

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. Refer the documentation.

One way to test is replace the call to header() in your code with a simple echo with some custom string (say 'XYZXYZ'). Then look at the raw output and check if there are any characters before this string.

SunnyPro
  • 175
  • 1
  • 8