【问题标题】:PHP mail form is not workingPHP 邮件表单不工作
【发布时间】:2013-11-28 17:39:27
【问题描述】:

我的电子邮件 php 表单有问题,当我单击提交按钮时,另一个页面显示为 There was a problem with your e-mail () 我不知道我做错了什么?

这是我的代码:

html代码

<!-- Subscription Form -->
  <form class="email" action="form.php" method="post">
    <input class="get_notified" type="text" placeholder="Enter your email address ..."/>
    <button type="submit" class="go" /></form>
<!-- End Subscription Form -->

    </div>
  </div>
</body>

php代码

<?php
$to = "email@mydomain.com";
$from = "email@mydomain.com";

$headers = "From: " . $from . "\r\n";

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];


if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{ 
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
    echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
   echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
}

【问题讨论】:

  • 您的代码中的两条错误消息完全相同,因此您不会知道是哪个else 块触发了它。将其中一条消息更改为不同的内容,以找出哪个条件评估为假。
  • 尝试将代码数量减少到只有邮件功能,看看是否有效,如果有效,开始一点一点添加您的代码
  • Dulax 已为您提供了代码不起作用的原因,请将 name 属性添加到您的表单字段中

标签: php forms email


【解决方案1】:

您需要在 HTML 表单中添加一个 name="email" 字段,以便 PHP 能够使用 $_POST['email'] 获取它

<input class="get_notified" name="email" type="text" placeholder="Enter your email address ..."/>

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 2012-01-03
    • 2013-06-03
    • 2017-10-27
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多