-1

我有一个基本表单,我正在尝试重定向到与提交时的索引页面不同的页面。我只能得到这个:

 header("Location: http://www.mysite.com/thanks.php"); 

当我把它放在上面时工作

 <!doctype html>

但这在 IE 中不起作用,并且我收到 HTML5 验证错误。

这是我的代码: 在页面顶部:

   <?php
  include("functions/contactfunctions.php"); 



   ?>

再向下:

  <?php
    if($_POST['submitted'] == "contactus")
    {
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];

    $email = $_POST['email'];
    $message = $_POST['message'];
    $location = $_POST['location'];
            if(!$firstname)
    $error = "Please enter your first name.";
    else
    if(!$lastname)
    $error = "Please enter your last name.";
    else
    if(!$email)
    $error = "Please enter a valid email.";
    else
    if(!$message)
    $error = "Please enter your message.";
    else
    if(!$location)
    $error = "Please tell us where you're from.";


    }
  ?>

    <?php
       if($_POST['submitted'])
       {
      ContactMessage($firstname, $lastname, $email, $message, $location);
        header("Location:http://www.mywebsite.com/thanks.php");
       exit;
    }

 ?> 

这是联系functions.php:

 <?php 
 include_once("databasefunctions.php");

 $contactsdbtable = "contacts";

 function GetHeaders()
 {
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    // Additional headers
    $headers .= "To: {$firstname} <{$email}>" . "\r\n";
    $headers .= 'From: My Site <noreply@mysite.com>' . "\r\n";
    return $headers;
 }
 function ContactMessage($firstname, $lastname, $email, $message, $location)
 {
global $contactsdbtable;
openDatabase();
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$email = mysql_real_escape_string($email);
$message = mysql_real_escape_string($message);
$location = mysql_real_escape_string($location);

$result = QuickQuery("INSERT INTO {$contactsdbtable}(firstname, lastname, email,         message, location) 
                      VALUES('{$firstname}', '{$lastname}', '{$email}', '{$message}',  '{$location}')");

if($result)
   {
    $headers = GetHeaders();
    $message = "\"Thank you for contacting us blah blah. We will be        answering your website inquiry post haste.\"<br />
    <br />
    <br />
    Best Regards,<br />
    <br />
    Us
    ";
    mail($email, "RE: Website Inquiry", $message, $headers);
    mail("myemail@blank.com", "Website Inquiry", "{$firstname}, {$email}, has sent a web design inquiry", $headers);

     }
   }


 ?>

我也尝试在索引页面和contactfunctions.php上添加无济于事。提交时,我被重定向回 index.php,这是表单上的操作。

在此先感谢您的帮助!

4

3 回答 3

2

你在开场前有空格<?php。在发送标头之前,您不能拥有任何内容,其中包括空格。

于 2012-07-30T01:05:05.140 回答
0

删除之前的空格和自动换行

<?php

甚至空格也是发送到导航器的信息,因此标题中不再存在。

编辑:答案已经给出。

于 2012-07-30T01:17:52.590 回答
-1

如果标头();没有重定向页面,那么这意味着表单没有按应有的方式提交。我建议您检查表单中的所有输入,而不是检查 $_POST['submitted']。

if($_POST['email']&&$_POST['message']/* And so on... */){
   echo "echo something here so you know it ACTUALLY HAS been submitted.";
}

提示:当使用 header(); 确保你使用 ob_start(); 和 ob_flush(); 像这样:

//Start of script
ob_start();

//Now header(); will work efficiently here inside your functions...

//End of script
ob_flush();
于 2012-07-30T02:52:15.840 回答