我不确定是否可以问这个问题。但有人可以帮我编辑这段代码,以便将它重定向到我的主页。我知道我需要设置标头位置只是不确定究竟在哪里导致我对php完全陌生。这是代码。
<?php
$from = $_POST['Name'];
$sendTo = $_POST['Email'];
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'email' => 'Email', 'message' => 'Message'); //
$okMessage = 'Contact form share button submited. Url of page will arrive in few seconds!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
$link = $_SERVER['HTTP_REFERER'];
try
{
$emailText = "$link You got message from shared button.\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
?>
答案 0 :(得分:-1)
你可以添加
header("Location: page.php");
在
之后$responseArray = array('type' => 'success', 'message' => $okMessage);
或之后
echo $responseArray['message'];