【问题标题】:php coding error not returning to main pagephp编码错误未返回主页
【发布时间】:2016-07-18 11:23:01
【问题描述】:

我是 PHP 新手,所以请温柔一点。

我编写了一个网站,并添加了订阅时事通讯部分。用户在按下提交之前添加他们的姓名和电子邮件地址。我收到了电子邮件,所以大部分都在工作,但表格并没有让我回到网站的主页。谁能指出我哪里出错了?

提前致谢

网站上的代码是:

<form name="frmcontact" id="frmcontact" action="php/contact.php" method="post">
      <input id="name" name="name" type="text" value="Name"
      onblur="this.value=(this.value=='') ? 'Name' : this.value;"              onfocus="this.value=(this.value=='Name') ? '' : this.value;" />
 <input id="email" name="email" type="text" value="Email" 
 onblur="this.value=(this.value=='') ? 'Email' : this.value;" onfocus="this.value=(this.value=='Email') ? '' : this.value;" />
<input name="submit" id="send" type="submit" value="Message" />
</form>


The code in contact.php is:

<?php session_start();

if(!$_POST) exit;

    $address = 'email@emailaddress.com';
    $email_subject ="Add me to the mailing list";
    $email    = $_REQUEST['email'];
    $name     = $_REQUEST['name'];
    $subject  = "Please add me to the mailing list ";

        if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }


         $e_subject = 'You\'ve been contacted by ' . $name . '.';

         $msg  = "You have been contacted by $name with regards to $subject.\r\n\n";
         $msg .= "$message\r\n\n";
         $msg .= "You can contact $name via email, $email.\r\n\n";
         $msg .= "-------------------------------------------------------------------------------------------\r\n";

        if(@mail($address, $subject, $msg, "From: $email\r\nReturn-Path: $email\r\n"))
        {echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
         else
         {echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
    header('Location:http://www.websiteaddress/homepage.html');
?>

【问题讨论】:

  • 在此处发布您的代码
  • 命令回声线并尝试
  • 你试过我的步骤了吗?

标签: php html forms email


【解决方案1】:

删除header 之前的所有输出在header 之后使用exit 并且还考虑在下面给出空格

                 v
header('Location: http://www.websiteaddress/homepage.html');
exit;

【讨论】:

    【解决方案2】:

    使用正确的目录路径

    if(isset($_POST['submit'])){
       $address = 'email@emailaddress.com';
        $email_subject ="Add me to the mailing list";
        $email    = $_REQUEST['email'];
        $name     = $_REQUEST['name'];
        $subject  = "Please add me to the mailing list ";
    
            if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
    
    
             $e_subject = 'You\'ve been contacted by ' . $name . '.';
    
             $msg  = "You have been contacted by $name with regards to $subject.\r\n\n";
             $msg .= "$message\r\n\n";
             $msg .= "You can contact $name via email, $email.\r\n\n";
             $msg .= "-------------------------------------------------------------------------------------------\r\n";
    
            if(@mail($address, $subject, $msg, "From: $email\r\nReturn-Path: $email\r\n"))
            {echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
             else
             {echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
                header('Location: homepage.html');
    }
    

    【讨论】:

    • 我应该在哪里更改目录路径?
    【解决方案3】:

    在使用 header() 之前你不能输出任何东西

    HTTP-Headers 应该在服务器的任何输出之前发送。如果你之前有输出,服务器会输出一个警告,比如'Headers already been sent'

    <?php
     ob_start();
    
      echo "redirecting now ....";
      header('Location: http://www.websiteaddress/homepage.html');
      exit();
    
     ob_end_flush();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 2022-11-10
      相关资源
      最近更新 更多