如何正确使用php header(位置:)

时间:2017-06-08 19:50:18

标签: php header

我已经阅读了几个答案和问题,然而,我仍然无法让我的标题()工作。这只是一个简单的联系表单,这是我将客人发送到thankyou页面的最后一步。我错过了什么

  <?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;
    }

     ?>

而且我知道我的代码可能会更好,所以如果有关于简化或缩减的建议,我会很乐意欣赏它!

1 个答案:

答案 0 :(得分:0)

请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。 请参阅documentation

测试的一种方法是使用带有一些自定义字符串的简单回声(比如'XYZXYZ')替换代码中对header()的调用。然后查看原始输出并检查此字符串之前是否有任何字符。