【问题标题】:php form won't send or go to header location after submit提交后php表单不会发送或转到标题位置
【发布时间】:2013-02-04 00:09:00
【问题描述】:

我有一个用于联系目的的简单 php 表单。但是,提交后它不会发送电子邮件或转到正确的页面。它会重定向到 mail.php。

我的联系人表单名为contact.php如下:

 <form id="contact-form" action="mail.php" method="POST">
    <fieldset>

        <label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
        <label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
        <label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
        <div class="wrapper">
            <div class="text-form">Your Message:</div>
            <textarea name="message" rows="6" cols="25"></textarea><br />
            <div class="clear">
        </div>
        <div class="buttons">
        <a class="button" href="#"><input class="button" type="submit" value="Send"></a>
        <a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
        </div>

    </fieldset>
</form>

而名为mail.php的php代码如下:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    &contact = $_POST['contact'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "info@whatever.co.za";
    $subject = "Contact form message";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    header("Location:contact.php");
    ?>

我做错了什么。它只是不会将消息发送到电子邮件或重定向到正确的页面??

【问题讨论】:

  • 你有什么错误吗?
  • 分号后有空格? "Location: contact.php" ?您确定您的网址正确吗?
  • @slugonamission。不,当它重定向到 mail.php 时,我没有收到任何错误,只有一个空白页面。
  • 而且网址是正确的。

标签: php forms submit form-submit


【解决方案1】:

尝试在邮件功能前使用@

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info@whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
@mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>

即使邮件功能产生任何问题,这也会让您通过。另外请检查是否有任何其他标头未使用http://php.net/manual/en/function.headers-sent.php 函数发送。

我强烈建议使用 PHPMailer 或其他一些类来发送任何类型的电子邮件。

【讨论】:

    【解决方案2】:

    你的语法有点错误。第 5 行的 &amp;contact 应该是 $contact。我假设您在生产服务器上,因此错误报告将被禁用并且您不会收到任何警告。

    【讨论】:

    • 感谢您对语法错误的通知。它总是最小的愚蠢错误,需要一生来纠正。除了将语法更改为正确之外,我还在邮件前添加了一个“@”,它可以工作。
    【解决方案3】:

    将mail.php改成如下:

    <?php
    mail("info@whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
    header("Location: contact.php");
    ?>
    

    【讨论】:

      【解决方案4】:

      尝试在脚本开头使用ob_start(),然后exit();在 header("Location:contact.php");

      之后

      像这样……

      <?php
          ob_start();
          $name = $_POST['name'];
          $email = $_POST['email'];
          $message = $_POST['message'];
          &contact = $_POST['contact'];
          $formcontent="From: $name \n Message: $message";
          $recipient = "info@whatever.co.za";
          $subject = "Contact form message";
          $mailheader = "From: $email \r\n";
          mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
          header("Location: contact.php");
          exit();
      ?>
      

      【讨论】:

      • 我刚刚尝试过,但无济于事。我要开始拔头发吗??要发送一个简单的联系表真是太难了。
      • 是否重定向到contact.php ??
      【解决方案5】:

      我认为从您的服务器发送邮件可能存在问题。 mail.php 文件可能会在邮件功能上出错并且没有进行重定向。

      创建一个仅包含 php 邮件功能的新文件(带有您的电子邮件地址和测试消息)并从您的网络浏览器浏览它,它会发送吗?

      【讨论】:

        【解决方案6】:

        请检查contact.php文件是否发生了从contact.php到mail.php的重定向

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-14
          • 2017-09-20
          • 2012-05-21
          • 1970-01-01
          • 1970-01-01
          • 2016-10-05
          相关资源
          最近更新 更多