【问题标题】:PHP Contact Form not submitting and shows a white screen [duplicate]PHP联系表未提交并显示白屏[重复]
【发布时间】:2019-09-10 16:56:30
【问题描述】:

我是 PHP 新手,我正在为我的网站制作联系表格。但是,每当我填写并提交表格时,它都会将我带到一个空白页面,并且我从未收到过电子邮件。当我的网站上线时,我已经对其进行了测试。如果重要的话,我的主机是无限免费的。

我尝试了故障排除,例如对代码进行小幅修改(更改一些名称等),但没有任何效果。

<html>
<section id="contact">
  <div class="container-contact">
<div style="text-align:center">
  <h3> Contact Me </h3>
      <p> I will get back to you shortly. </p>
</div>
<div class="row">
      <div class="column">
    <form id="contact-form" method="post" action="contactform.php">
      <label for="firstname">First Name</label>
      <input type="text" id="firstname" placeholder="Your first name..." required>
      <label for="lastname">Last Name</label>
      <input type="text" id="lastname" placeholder="Your last name..." required>
      <label for="email">Email Address</label>
      <input type="email" id="email" placeholder="Your email..." required>
      <label for="message">Message</label>
      <textarea id="message" name="message" placeholder="Write your message..." style="height:170px" required></textarea>
      <input type="submit" name="submit" value="Submit">
    </form>
  </div>
</div>
  </div>
</section>
</html>

<?php
  $firstname = $_POST['firstname']
  $lastname = $_POST['lastname']
  $user_email = $_POST['email'];
  $message = $_POST['message']

  $email_from = 'myemail@domain.com'

  $email_subject = "New Form Submission from $firstname.\n";

  $email_body = "First Name: $firstname.\n".
                  "Last Name: $lastname.\n".
                    "Email: $user_email.\n".
                      "Message: $message.\n";

  $to = "myemail@domain.com";

  $headers = "From: $email_from \r\n";
  $headers .= "Reply To: $user_email \r\n";

  mail($to,$email_subject,$email_body,$headers);

  header("Location: contact.html");

?>

HTML 文件名为contact.html,PHP 文件名为contactform.php

表单没有提交,我也没有收到电子邮件。难道我做错了什么?我需要设置其他东西吗?

提前致谢,

托马斯

【问题讨论】:

  • 您检查浏览器控制台是否有错误?您是否检查过 Web 服务器日志是否有错误?是否在服务器上设置了邮件服务来发送邮件?

标签: php html email


【解决方案1】:

猜测您遇到了未定义的索引错误,并且您的环境未设置为显示错误。我会将name 属性添加到缺少它的input 字段作为开始。

例如名字字段

 <input type="text" id="firstname" placeholder="Your first name..." required>

应该是

 <input type="text" name="firstname" id="firstname" placeholder="Your first name..." required>

此外,您应该查看打开错误或查看日志文件。

【讨论】:

  • 我打开了显示错误并按照您的建议添加了名称。显示错误让我发现我错过了一些“;”。现在我不再看到一个空白屏幕,而是像我在 PHP 代码中指定的那样将我发送回联系页面。但是我没有收到邮件。我还在做错什么吗?我的代码和以前一样,除了 ;添加了没有,我在 HTML 中添加了名称。
  • @Thomas 我查看了您的托管服务提供商,似乎他们限制了使用 PHP 的 mail() 函数可以完成的操作(请参阅infinityfree.net/support/php-mail)。我的猜测是这就是你没有收到电子邮件的原因。如果您有 GMail 帐户,则可以使用他们的说明进行设置,代码与您已有的代码不会有太大不同。
【解决方案2】:

由于 PHP 解析错误,您得到空白页。您需要在 PHP 中使用分号(;) 来结束语句。 例如,而不是, $firstname = $_POST['firstname'] 它应该是, $firstname = $_POST['firstname'];

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    相关资源
    最近更新 更多