【问题标题】:PHP form is not successfully submitting getting custom errorPHP表单未成功提交获取自定义错误
【发布时间】:2018-07-29 20:30:26
【问题描述】:

我的 PHP 表单没有成功提交。我不断收到我写的自定义错误(“糟糕,出现问题。请重试”)。

任何帮助将不胜感激。我对 PHP 完全陌生,所以我想也许我的一些 php 变量链接错误并且没有连接到我的 mailer-new.php 文件?

提前致谢,

   <section class="form-body">
    <form method="post" action="mailer-new.php" class="contact-form" >
        <div class="row">
            <?php
                if ($_GET['success']== 1){
                    echo " <div class=\"form-messages success\"> Thank you! 
                your message has been sent. </div>";
                }
                if ($_GET['success']== -1){
                    echo " <div class=\"form-messages error\"> Opps there was a 
                  problem. Please try again </div>";
                };
                ?>
            <div class="field name-box">
                <input type="text" name="name" id="name" placeholder="Who 
                    Are You?" required/>
                <label for="name">Name</label>
                <span class="ss-icon">check</span>
            </div>
            <div class="field email-box">
                <input type="text"  name="email" id="email" 
                    placeholder="name@email.com" required/>
                <label for="email">Email</label>
                <span class="ss-icon">check</span>
            </div>
            <div class="field msg-box">
                <textarea name="message" id="msg" rows="4" 
                    placeholder="Your message goes here..."/></textarea>
                <label for="message">Msg</label>
                <span class="ss-icon">check</span>
            </div>
            <input class="button" type="submit" value="Send"/>
        </div>
    </form>
</section>

MAILER.PHP

<?php

// Get the form fields, removes html tags and whitespace.
$name    = strip_tags(trim($_POST["name"]));
$name    = str_replace(array("\r","\n"), array(" ", " " ), $name);
$email   = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: http://www.conallen.ie/index.php?
 success=-1#form");
    exit;
}

// Set the recipient email address. Update this to YOUR desired email address.
$recipient = "allenconallen46@gmail.com";

// Set the email subject.
$subject = "New contact from $name";

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";

// Build the email headers.
$email_headers = "From: $name <$email>";

// Send the email.
mail($recipient, $subject, $email_content, $email_headers);

// Redirect to the index.html page with success code
header("Location: http://www.conallen.ie/index.php?success=1#form");

?>    

【问题讨论】:

  • 您的代码看起来很合理,尽管您不需要在电子邮件地址上调用 filter_var 两次。我建议您在“检查数据步骤”之前对其进行调试以查看值中的内容。例如。 var_dump($name, $email, $message);exit;
  • @TimFountain 感谢您的回复,我已显示响应警报消息。但是现在电子邮件没有到达我的收件箱。我不知道他们为什么不发送。我现在就试试你的建议 - 谢谢

标签: php html


【解决方案1】:

此代码在我的本地计算机上正常工作,即使未发送电子邮件,响应消息也正确发送。我所做的更改是更改主机名并将两行标题重定向到失败响应中的一行。您还在 HTML 中提到了文件名action="mailer-new.php",问题中提到的文件名是MAILER.PHP。它们在代码中是否相同?

要检查邮件是否已发送,请删除标头重定向并像这样更新。如果您收到失败的响应,则表示您的服务器中未配置邮件。

if(mail($recipient, $subject, $email_content, $email_headers)) {
echo "mail sent";
} else {
 echo "mail sent failed";
}

您也可以使用 SMTP 发送电子邮件。您可以使用一个名为 PHP Mailer 的库,并且可以使用任何有效的电子邮件地址,例如 gmail 帐户。如果是这种情况,请看看这个问题

Sending email with PHP from an SMTP server

【讨论】:

  • 感谢您的回复。我现在已经收到响应成功消息。但不幸的是,这些电子邮件没有登陆我的电子邮件帐户。你知道他们为什么不发送吗?同样是的,文件名是 mailer-new.php 很抱歉造成混淆。非常感谢
  • 更新了答案。请检查一下。
  • 我更新了标头重定向,它通知我电子邮件已发送。但它仍然没有出现在我的收件箱中。再次感谢。
  • 您在垃圾邮件文件夹中检查过吗?如果它仍然不起作用,您必须尝试使用​​ SMTP gmail。它应该可以工作。
  • 是的,我已经检查过我的垃圾邮件文件夹好几次了。我也看过一些教程,但仍然没有出现电子邮件。似乎代码正在以某种方式工作,但是电子邮件却无法正确发送。生病谷歌 SMTP Gmail 现在。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2012-01-11
  • 2021-06-27
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 2019-03-12
相关资源
最近更新 更多