0

我有用于发送邮件的 php 脚本,在这个脚本的末尾我重定向到“谢谢页面”。但我有问题。如果我在页面上提交邮件表单,邮件发送正确但重定向不起作用,但如果我直接去邮件发送脚本重定向的地址工作正常。

错误在哪里?

<?php
include('includes/config.php'); 
if(isset($_POST['Email_Orderer_Form'])) {
$headers = "From: xy@xy.com\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/plain; charset=utf-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "Return-Path:  xy@xy.com\n";
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = " xy@xy.com, ".$_POST['Email_Orderer_Form']."";  // This email address will recieve the data of form.html
$email_subject = "XY";  // This would be the subject of email that you will recieved
$White_PHP = $_POST['checkboxWhite'];
$White_Velikost_PHP = $_POST['White_Velikost_Form'];
$White_Ks_PHP = $_POST['White_Ks_Form'];
$Black_PHP = $_POST['checkboxBlack'];
$Black_Velikost_PHP = $_POST['Black_Velikost_Form'];
$Black_Ks_PHP = $_POST['Black_Ks_Form'];
$Jmeno_PHP = $_POST['Jmeno_Form'];
$Prijmeni_PHP = $_POST['Prijmeni_Form'];
$Ulice_PHP = $_POST['Ulice_Form'];
$Cislo_Popisne_PHP = $_POST['Cislo_Popisne_Form'];
$Mesto_PHP = $_POST['Mesto_Form'];
$PSC_PHP = $_POST['PSC_Form'];
$Email_Orderer_PHP = $_POST['Email_Orderer_Form'];
$Telefon_PHP = $_POST['Telefon_Form'];
$Komentar_PHP = $_POST['Komentar_Form'];
function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$Stav = "New";
$White_PHP = mysql_real_escape_string($White_PHP);
$White_Velikost_PHP = mysql_real_escape_string($White_Velikost_PHP);
$White_Ks_PHP = mysql_real_escape_string($White_Ks_PHP);
$Black_PHP = mysql_real_escape_string($Black_PHP);
$Black_Velikost_PHP = mysql_real_escape_string($Black_Velikost_PHP);
$Black_Ks_PHP = mysql_real_escape_string($Black_Ks_PHP);
$Jmeno_PHP = mysql_real_escape_string($Jmeno_PHP);
$Prijmeni_PHP = mysql_real_escape_string($Prijmeni_PHP);
$Ulice_PHP = mysql_real_escape_string($Ulice_PHP);
$Cislo_Popisne_PHP = mysql_real_escape_string($Cislo_Popisne_PHP);
$Mesto_PHP = mysql_real_escape_string($Mesto_PHP);
$PSC_PHP = mysql_real_escape_string($PSC_PHP);
$Email_Orderer_PHP = mysql_real_escape_string($Email_Orderer_PHP);
$Telefon_PHP = mysql_real_escape_string($Telefon_PHP);
$Komentar_PHP = mysql_real_escape_string($Komentar_PHP);
if (($_POST['checkboxWhite'] == 'Bílé tričko') && ($_POST['checkboxBlack'] == 'Černé tričko')) {
    $email_message .= "Objednávka\n";
    $email_message .= "Tričko 1: ".clean_text($White_PHP)."\n";
    $email_message .= "Velikost: ".clean_text($White_Velikost_PHP)."\n";
    $email_message .= "Kusů: ".clean_text($White_Ks_PHP)."\n\n";
    $email_message .= "Tričko 2: ".clean_text($Black_PHP)."\n";
    $email_message .= "Velikost: ".clean_text($Black_Velikost_PHP)."\n";
    $email_message .= "Kusů: ".clean_text($Black_Ks_PHP)."\n\n";
    $email_message .= "Odběratel\n";
    $email_message .= "Jméno: ".clean_text($Jmeno_PHP)."\n";
    $email_message .= "Příjmení: ".clean_text($Prijmeni_PHP)."\n";
    $email_message .= "Ulice: ".clean_text($Ulice_PHP)."\n";
    $email_message .= "Číslo popisné: ".clean_text($Cislo_Popisne_PHP)."\n";
    $email_message .= "Město: ".clean_text($Mesto_PHP)."\n";
    $email_message .= "PSČ: ".clean_text($PSC_PHP)."\n";
    $email_message .= "Email: ".clean_text($Email_Orderer_PHP)."\n";
    $email_message .= "Telefon: ".clean_text($Telefon_PHP)."\n";
    $email_message .= "Komentář: ".clean_text($Komentar_PHP)."\n";
    mysql_query("INSERT INTO objednavky (Tricko1,Tricko1Velikost,Tricko1Ks,Tricko2,Tricko2Velikost,Tricko2Ks,Jmeno,Prijmeni,Ulice,CisloPopisne,Mesto,PSC,Email,Telefon,Zprava,Stav) 
                VALUES ('$White_PHP','$White_Velikost_PHP','$White_Ks_PHP','$Black_PHP','$Black_Velikost_PHP','$Black_Ks_PHP','$Jmeno_PHP','$Prijmeni_PHP','$Ulice_PHP','$Cislo_Popisne_PHP','$Mesto_PHP','$PSC_PHP','$Email_Orderer_PHP','$Telefon_PHP','$Komentar_PHP','$Stav')")or die(mysql_error());
}
else {
if ($_POST['checkboxWhite'] == 'Bílé tričko') {
    $Black_PHP = "-";
    $Black_Velikost_PHP = "-";
    $Black_Ks_PHP = "-";
    $email_message .= "Objednávka\n";
    $email_message .= "Tričko: ".clean_text($White_PHP)."\n";
    $email_message .= "Velikost: ".clean_text($White_Velikost_PHP)."\n";
    $email_message .= "Kusů: ".clean_text($White_Ks_PHP)."\n\n";
    $email_message .= "Odběratel\n";
    $email_message .= "Jméno: ".clean_text($Jmeno_PHP)."\n";
    $email_message .= "Příjmení: ".clean_text($Prijmeni_PHP)."\n";
    $email_message .= "Ulice: ".clean_text($Ulice_PHP)."\n";
    $email_message .= "Číslo popisné: ".clean_text($Cislo_Popisne_PHP)."\n";
    $email_message .= "Město: ".clean_text($Mesto_PHP)."\n";
    $email_message .= "PSČ: ".clean_text($PSC_PHP)."\n";
    $email_message .= "Email: ".clean_text($Email_Orderer_PHP)."\n";
    $email_message .= "Telefon: ".clean_text($Telefon_PHP)."\n";
    $email_message .= "Komentář: ".clean_text($Komentar_PHP)."\n";
    mysql_query("INSERT INTO objednavky (Tricko1,Tricko1Velikost,Tricko1Ks,Tricko2,Tricko2Velikost,Tricko2Ks,Jmeno,Prijmeni,Ulice,CisloPopisne,Mesto,PSC,Email,Telefon,Zprava,Stav) 
                VALUES ('$White_PHP','$White_Velikost_PHP','$White_Ks_PHP','$Black_PHP','$Black_Velikost_PHP','$Black_Ks_PHP','$Jmeno_PHP','$Prijmeni_PHP','$Ulice_PHP','$Cislo_Popisne_PHP','$Mesto_PHP','$PSC_PHP','$Email_Orderer_PHP','$Telefon_PHP','$Komentar_PHP','$Stav')")or die(mysql_error());
}
if ($_POST['checkboxBlack'] == 'Černé tričko') {
    $White_PHP = "-";
    $White_Velikost_PHP = "-";
    $White_Ks_PHP = "-";
    $email_message .= "Objednávka\n";
    $email_message .= "Tričko: ".clean_text($Black_PHP)."\n";
    $email_message .= "Velikost: ".clean_text($Black_Velikost_PHP)."\n";
    $email_message .= "Kusů: ".clean_text($Black_Ks_PHP)."\n\n";
    $email_message .= "Odběratel\n";
    $email_message .= "Jméno: ".clean_text($Jmeno_PHP)."\n";
    $email_message .= "Příjmení: ".clean_text($Prijmeni_PHP)."\n";
    $email_message .= "Ulice: ".clean_text($Ulice_PHP)."\n";
    $email_message .= "Číslo popisné: ".clean_text($Cislo_Popisne_PHP)."\n";
    $email_message .= "Město: ".clean_text($Mesto_PHP)."\n";
    $email_message .= "PSČ: ".clean_text($PSC_PHP)."\n";
    $email_message .= "Email: ".clean_text($Email_Orderer_PHP)."\n";
    $email_message .= "Telefon: ".clean_text($Telefon_PHP)."\n";
    $email_message .= "Komentář: ".clean_text($Komentar_PHP)."\n";
    mysql_query("INSERT INTO objednavky (Tricko1,Tricko1Velikost,Tricko1Ks,Tricko2,Tricko2Velikost,Tricko2Ks,Jmeno,Prijmeni,Ulice,CisloPopisne,Mesto,PSC,Email,Telefon,Zprava,Stav) 
                VALUES ('$White_PHP','$White_Velikost_PHP','$White_Ks_PHP','$Black_PHP','$Black_Velikost_PHP','$Black_Ks_PHP','$Jmeno_PHP','$Prijmeni_PHP','$Ulice_PHP','$Cislo_Popisne_PHP','$Mesto_PHP','$PSC_PHP','$Email_Orderer_PHP','$Telefon_PHP','$Komentar_PHP','$Stav')")or die(mysql_error());
    mysql_Close();
}
}
@mail($email_to, $email_subject, $email_message, $headers);
}
header('Location: order_send.php');
die();
?>
4

2 回答 2

2

根据 header() 手册: 请记住,必须在发送任何实际输出之前调用 header(),无论是通过普通 HTML 标记、文件中的空白行还是从 PHP 发送。使用 include 或 require 函数或其他文件访问函数读取代码并在调用 header() 之前输出空格或空行是一个非常常见的错误。使用单个 PHP/HTML 文件时也存在同样的问题。

据我所见,您的 php 文件没有任何真正的 html 输出,我认为以下代码可以工作[!!未测试,因为我很懒]

<?php 
//ob_start even before your includes
ob_start( );

//add ob_end_clean before your header
ob_end_clean( ); 
header( 'Location: http://www.google.com' );

//die() might work too
exit();
?> 
于 2013-09-11T11:24:52.750 回答
0

尝试在 php 脚本的顶部添加这一行:

error_reporting(E_ALL); //it will show all errors

然后你会在屏幕上或在 php 错误日志中看到有什么问题。我打赌您会看到:警告:无法修改标头信息 - 标头已由 ... 等发送。

如果这是您的问题,请阅读此内容以更正问题:如何修复 PHP 中的“标头已发送”错误

在其他情况下,请发布错误消息以更好地分析您的问题。

结核病

于 2013-09-11T11:21:04.223 回答