【问题标题】:PHP Contact Form returning error upon submissionPHP联系表单在提交时返回错误
【发布时间】:2015-06-08 14:34:38
【问题描述】:

所以我尝试在 2 个不同的服务器上发布一个带有 PHP 联系表单的页面,但我无法让它们中的任何一个完美地工作。

服务器 1:
提交时返回500 internal error 页面。
我联系了网络支持,他们回复说我有:

脚本“index.php”的标头格式错误:标头错误:

我的 index.php 代码如下:

    <?php
 if(isset($_POST['submit']))
 {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

 $to="testemail@email.com";
 $subject="test subject";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: ".$email_from."\r\n";
 $message="   

         Name:
         $name     
         <br>
         Email:
         $email        
         <br>
         Message:
         $query        

   ";
    if(mail($to,$subject,$message,$headers))
        header("Location:../contact.php?msg=Successful Submission!<br />
 Thank you for contacting us.");
    else
        header("Location:../contact.php?msg=Error To send Email !");
 }
?>

服务器 2:
正确地将我链接到我的“感谢页面”,但它正在显示 “发送电子邮件出错!”代替 “提交成功!”

任何帮助或建议将不胜感激!

【问题讨论】:

  • 除了下面的答案中提到的(1.在header("Location: ...);和2.urlencode()你的查询字符串中使用绝对URI),如果你打算发送标题,你会想要修剪 PHP 开始标记之前的前导空格。
  • 除了答案之外,请确保您上面提供的代码出现在您页面的最顶部。

标签: php html forms contact


【解决方案1】:

我认为问题出在这部分:

 if(mail($to,$subject,$message,$headers))
        header("Location:../contact.php?msg=Successful Submission!<br />
 Thank you for contacting us.");
    else
        header("Location:../contact.php?msg=Error To send Email !");

具体在header();

  1. 每个空格都将转换为 %20,因为没有带空格的 URL;
  2. 没有像 , ! 这样的特殊字符在网址中
  3. &lt;br /&gt; 中的“/”会建议一个名为“>感谢您与我们联系”的文件。

签出:http://www.w3schools.com/tags/ref_urlencode.asp;

还要在每个标头后面加上exit();,以防止脚本进一步执行

【讨论】:

  • @Melvallous 没问题
【解决方案2】:

问题似乎是标头位置重定向调用,它应该提供完整的 url,例如:

header("Location: http://www.example.com");

相对 url 会根据浏览器的最大努力进行处理,并且您的浏览器显然不喜欢 ../,因此会出现“错误标头”投诉。

根据official URL spec,这在php.net 页面上注明。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2014-12-19
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    相关资源
    最近更新 更多