【问题标题】:php form submission is working but erroring instead of redirecting [duplicate]php表单提交工作但出错而不是重定向[重复]
【发布时间】:2014-08-21 08:34:25
【问题描述】:

谁能检查这个 php 代码并告诉我为什么会出现这个错误:

"Warning: Cannot modify header information - headers already sent by (output started at /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php:144) in /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php on line 146"

PHP:

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "Jonathansumner90@gmail.com";
    $email_subject = "contact from Designs by Gabe form";

    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['telephone']) ||
        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  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 />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->
 <?php
 header("Location: thankyou.html");
 ?>




<?php

}
?>

HTML:

<form action="send_form_email.php" method="post" name="contactform">



                <label for="first_name">First Name *</label>
                <input type="text" name="first_name" size="30" maxlength="50" />


                <label for="last_name">Last Name *</label>
                <input type="text" name="last_name" size="30" maxlength="50" />

                <label for="email">Email Address *</label>
                <input type="text" name="email" size="30" maxlength="80" />


                <label for="telephone">Telephone Number</label>
                <input type="text" name="telephone" size="30" maxlength="30" />

                <label for="comments">Comments *</label>
                <textarea name="comments" rows="6" cols="25"></textarea>

                <input id="submit" style="margin-right: 30px;" type="submit"     value="Submit" />

之前有人问过这个问题,但不是在这种情况下。消息通过但没有重定向它给我一个错误。我对其他网站使用相同的表单提交代码,它工作正常。

【问题讨论】:

  • 第 144 行是哪一行??
  • 有数百个问题解释了导致该错误的原因以及如何修复它。您在发帖前是否尝试过搜索?
  • 不要在 header() 上方添加 HTML 输出。 &lt;!-- include your own success html here --&gt; 是问题所在。
  • 是的,我读过其他类似的问题。他们都没有解决你的问题。
  • 第 144 行是标题周围的开始 php 标签所在的位置

标签: php jquery html css forms


【解决方案1】:

试试这个:

<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "Jonathansumner90@gmail.com";

    $email_subject = "contact from Designs by Gabe form";





    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['telephone']) ||

        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $email_from = $_POST['email']; // required

    $telephone = $_POST['telephone']; // not required

    $comments = $_POST['comments']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  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 />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Email: ".clean_string($email_from)."\n";

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
header("Location: thankyou.html");


}
?>

【讨论】:

    【解决方案2】:

    将 header 命令放在第一个 PHP 标记中

    @mail($email_to, $email_subject, $email_message, $headers);  
    header("Location: thankyou.html");
    ?>
    

    【讨论】:

      【解决方案3】:

      你做错了。输出某些内容(甚至不是空格)后,您无法设置标题。只需在顶部设置标题即可

         <?php
           header("Location: thankyou.html");
      

      或使用 html 元重定向

       <META http-equiv="refresh" content="0;URL=http://example.com/thankyou.html">
      

      或使用javascript

      window.location.replace("http://example.com/thankyou.html"); 
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        标题周围的开始 php 标记之前的空白是一个问题。我不得不假设这是我挑剔的服务器。我听说过空白问题,但我从未遇到过这种问题,而且我已经多次使用过这种形式。

        感谢大家帮助我。

        【讨论】:

          【解决方案5】:

          当您重定向到某个页面时,在 header('location:..'); 之后使用 exit

          喜欢

          header("Location: thankyou.html");
          exit;
          

          还有*Not To print any think before it*

          比如你在php中使用html的评论点赞

          &lt;!-- include your own success html here --&gt;

          删除以上行或将其设为 php 注释为

          // include your own success html here

          【讨论】:

          • died() 是他定义的函数
          • @Jenz 和 Debflav 我改变了答案。
          • 退出不起作用...
          • @user3700610 阅读更新的答案。您的页面上打印了一些输出。这就是为什么会出现这个错误。尝试评论标题,然后看看你会在页面上看到一些输出。所以在调用header('location:..)之前删除所有输出或处理它们
          • 我删除了 html 评论。这也不起作用。
          猜你喜欢
          • 2015-12-11
          • 1970-01-01
          • 2014-01-15
          • 1970-01-01
          • 1970-01-01
          • 2011-11-24
          • 2016-10-05
          • 1970-01-01
          相关资源
          最近更新 更多