【问题标题】:PHP Redirect Email ContactPHP 重定向电子邮件联系方式
【发布时间】:2023-03-20 18:10:01
【问题描述】:

我正在测试来自本地服务器上提交的联系人。 dev.localhost

我得到的错误是

The requested URL /volunteer-success.php was not found on this server.

我是 PHP 新手。我正在尝试提交表单,通过电子邮件发送消息+名称,然后将用户重定向到感谢屏幕。

我的 HTML 如下...

<form action="volunteer-submit.php" method="post" name="contact_form" data-parsley-validate>
    <textarea name="message" id="message" placeholder="Message" required></textarea> 
    <input type="text" placeholder="Your Name" id="name" name="name" required>
    <input type="email" placeholder="Your Email Address" id="email" name="email" required>
    <input type="submit" class="submit" value="Submit">
</form>

发送邮件的PHP如下

$to      = "example@example.com";
$subject = 'Email';
$headers = 'From: no-reply@example.com' . "\r\n" .
'Reply-To: '.$_POST["email"]. "\r\n" .
"Content-Type: text/html; charset=ISO-8859-1\r\n".
'X-Mailer: PHP/' . phpversion();

 $message = '<html><head><title></title></head><body>';
 $message .= '<p>Hi Ben, </p>';
 $message .= '<p>'.$_POST['message'].' </p>';
 $message .= '<p>'.$_POST['email'];
 $message .= '</body></html>';



mail($to, $subject, $message, $headers);

//Redirect

header("Location:volunteer-success.php"); /* Redirect browser */

?>

很想知道为什么它没有重定向。我已将 php 文件与 index.html 文件一起保存到 dev.localhost。

*使用的电子邮件示例

【问题讨论】:

  • "在此服务器上找不到请求的 URL /volunteer-success.php。"说文件在 apt 位置不可用。尝试对该文件进行绝对地址,例如。 header("位置:yourdomain.com/volunteer-success.php");
  • 尝试:header("Location: /volunteer-success.php");或: header("位置:./volunteer-success.php");
  • 您上面显示的代码是在实际的志愿者-submit.php 文件中还是包含在另一个文件中?
  • @Magicianred 谢谢! “位置:./volunteer-success.php”);工作
  • 顺便说一句,Location: 后面需要有一个空格,而您的示例代码中没有,这可能是错误。

标签: php html email redirect contact


【解决方案1】:

试试这个 -

header()之后使用exit();

因为在将页面重定向到另一个页面后,不应执行标题下方的代码。

header("Location: ./volunteer-success.php"); /* Redirect browser */
exit();

还有一件事。检查文件中前后的空间

 // Before shouldn't be any space
 <?php 


  ?>
// After shouldn't be any space as well

【讨论】:

  • 为了防止?&gt; 之后的任何空格,您可以简单地省略它。无论如何,PHP 都会在文件末尾停止,因此根本不关闭 PHP 标记通常更安全(因为编辑器会自动添加换行符)。
  • 是的,没错。但如果是header。如果您使用它来重定向,那么您需要检查所有内容,exit() 应该在header() 之后。
【解决方案2】:
header("Location: ./volunteer-success.php");

享受您的代码吧! :)

【讨论】:

  • 嘿@Magicianred 知道为什么电子邮件不会发送吗?它把我带到重定向页面,但没有发送电子邮件。我使用了正确的电子邮件,而不是上面使用的示例。
  • 如果配置正常,请尝试:rtcamp.com/tutorials/php/test-email-sending 然后尝试更改换行符的字符转义(它基于您的服务器):“\r\n”适用于 Windows 服务器“\n”适用于 unix 服务器,或者您尝试使用 ' 而不是 "
  • 谢谢。会试一试的。
【解决方案3】:

我猜你应该使用 javascript 重定向

<script>window.location.href="volunteer-success.php";</script>

因为,大多数时候 php Header() 不起作用

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 2014-06-09
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多