我的聯繫表格正在工作,但它不會將我重定向到我的感謝頁面。在我將它轉移到新主機之前,它正在工作。PHP的重定向不工作的聯繫表格(消息發送)
<!-- Contact PHP Stuff -->
<?php
session_start();
require_once 'libs/phpmailer/PHPMailerAutoload.php';
$errors = [];
if(isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['message'])) {
$fields = [
'name'=> $_POST['name'],
'email'=> $_POST['email'],
'subject'=> $_POST['subject'],
'message'=> $_POST['message']
];
foreach($fields as $field => $data) {
if(empty($data)) {
$errors[] = 'The ' . $field . ' field is required';
}
}
if(!empty($fields['name'])) {
if (!preg_match("/^[a-zA-Z ]*$/",$fields['name'])) {
$errors[] = "Name can only include letters A-Z";
}
}
if(!empty($fields['email'])) {
if (!filter_var($fields['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = "Please enter a valid email address";
}
}
if(empty($errors)) {
$mail = new PHPMailer(); // create a new object
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "*****";
$mail->SetFrom($fields['email'], $fields['name']);
$mail->Subject = $fields['subject'];
$mail->Body = $fields['message'];
$mail->AddAddress("[email protected]");
if($mail->Send()) {
header('Location: thanks');
die();
} else {
$errors[] = "Sorry could not send message. Try again later";
}
}
} else {
$errors[] = 'Something went wrong';
}
$_SESSION['errors'] = $errors;
$_SESSION['thanks'] = $thanks;
$_SESSION['fields'] = $fields;
header('Location: contact#contact-form');
?>
線header('Location: thanks');
被重定向我用我以前的主機thanks.html但現在即使正在發送的消息,並發表了這篇沒有發生。
我在教程中找到了代碼,無法記住它在一年前的位置。
什麼是錯誤?嘗試在啓動php標記'error_reporting(E_ALL)後添加到文件的頂部;'也......是什麼打印在頁面上?通常會導致標題無法設置,因爲已經發送了標題。 –