【问题标题】:PHP form processor script not redirectingPHP表单处理器脚本未重定向
【发布时间】:2023-04-04 16:26:01
【问题描述】:

我正在使用 PHP 处理器脚本制作一个 html 联系表单。 这是我的代码

HTML

    <form enctype="multipart/form-data" action="contactprocessor.php" method="post" class="wpcf7-form" onSubmit="return validatePage1();">
      <div class="form-row">
        <label class="label">Name*</label>
        <span class="wpcf7-form-control-wrap name">
        <input type="text" id=field_1 name=field_1 value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required text-input" aria-required="true" placeholder="Introduce yourself" />
        </span></div>
      <div class="form-row">
        <label class="label">Company</label>
        <span class="wpcf7-form-control-wrap company">
        <input type="text" name=field_2 id=field_2 value="" size="40" class="wpcf7-form-control wpcf7-text text-input" placeholder="Fly your company flag" />
        </span></div>
      <div class="form-row">
        <label class="label">Email* </label>
        <span class="wpcf7-form-control-wrap email">
        <input type="email" name=field_3 id=field_3 value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email text-input" aria-required="true" placeholder="Help us keep in touch" />
        </span></div>
      <div class="form-row">
        <label class="label">Phone</label>
        <span class="wpcf7-form-control-wrap phone">
        <input type="tel" name=field_4 id=field_4 value="" size="40" class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-tel text-input" placeholder="If you fancy a chat" />
        </span></div>
      <div class="form-row">
        <label class="label">Subject*</label>
        <span class="wpcf7-form-control-wrap subject">
        <input type="text" name=field_5 id=field_5 value="" size="40" class="wpcf7-form-control wpcf7-text text-input" placeholder="What’s on your mind?" />
        </span></div>
      <div class="form-row">
        <label class="label">Message*</label>
        <span class="wpcf7-form-control-wrap message">
        <textarea name=field_6 id=field_6 cols="40" rows="14" class="wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required" aria-required="true" placeholder="Share your thoughts and comments"></textarea>
        </span></div>
      <div class="form-row">
        <input id="saveForm" type="submit" value="Get In Touch" class="wpcf7-form-control wpcf7-submit btn--invert" />
      </div>

    </form>

这里是contactprocessor.php代码

<?php

  mail("jen@abhayayks.com",$_POST['field_5'],

  "Form data:

   Name: " . $_POST['field_1'] . "
   \nEmail: " . $_POST['field_3'] . " 
   \nPhone : " . $_POST['field_4'] . " 
   \nCompany : " . $_POST['field_2'] . "  
   \nMessage : " . $_POST['field_6'] . " 

    ");

    header("Location: contact.php");

   ?>

现在,当我提交表单时,我收到了确认 PHP 脚本正在运行的邮件。但我没有被重定向回contact.php(我也尝试过使用index.php,甚至http://www.google.com)。 能指出错误吗?

编辑: 现在,我从 php.ini 文件打开错误日志和错误报告,重新启动本地服务器,运行脚本,收到邮件,再次没有重定向,错误日志文件中没有错误。 我已将文件上传到我的在线服务器上。您可以确认问题。 Link

编辑 2 我只是注意到除了邮件功能之外什么都没有。我的意思是尝试过使用

echo '<script type="text/javascript">window.onload = function(){ alert("Thank you");}</script>';

这也不起作用。我还尝试使用 include("confirm.html"); 包含其他一些文件;并且该文件未包含在内。

【问题讨论】:

  • 开启 display_errors/error_reporting,你可能会收到“headers already sent”警告。
  • 您是否在 PHP 日志文件中收到任何错误?如果您不确定它在哪里,请创建一个仅包含&lt;?php phpinfo(); 的文件并运行它,然后搜索error_log。另外,请尝试在mail() 调用之前添加ini_set('display_errors', 'On');error_reporting(E_ALL);
  • @André:建议使用单个空格,但 RFC 没有要求。也就是说,他的浏览器可能会忽略这条线。
  • 您遇到了什么错误。对我来说它工作正常。

标签: php html validation


【解决方案1】:

header("位置:contact.php");应该是 header("Location:contact.php");

【讨论】:

  • 这个空间与它无关......至少我知道这个基础知识伙伴......
【解决方案2】:

看来代码是正确的

你检查过你的错误日志吗?

您的文件是什么字符集编码? (如果文件是 UTF-8 带有 BOM 时浏览器在 BOM 转到输出流后无法发送标头,则会出现问题)

【讨论】:

  • 是的,文件是UTF-8
  • 尝试在没有 BOM 的情况下将文件保存为 UTF-8(取决于您的文本编辑器) - 例如 windows 记事本无法做到 - 然后尝试以 ANSI 编码保存
【解决方案3】:

请尝试以下代码,您需要在 Location: 和 Contact.php 之间留出空格

<?php
header("Location: contact.php");
exit();
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多