我正在尝试重定向PHP联系表单电子邮件脚本,以便在提交后将用户重定向到新页面。目前,它发送电子邮件,并在页面底部输出:[“”]
我尝试在底部添加它,但它给了我一个错误:
header("Location: page2.php");
我使用的PHP看起来像这样:
<?php
function send_json( $success = true, $data = '') {
$response = array('');
if ( ! empty($data)) {
$response['data'] = $data;
}
echo json_encode($response);
die;
}
function validate($name, $email) {
$errors = array();
if (null == $name) {
$errors['name'] = 'Enter your name';
}
if (null == $email || false === $email) {
$errors['email'] = 'Enter correct email';
}
// If errors - die
if ( ! empty($errors)) {
send_json(false, $errors);
}
}
if (empty($_POST)) {
exit;
}
// Load Config file
$ini = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.ini';
$config = parse_ini_file( $ini );
// Receive and validate data
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING );
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$company = filter_input(INPUT_POST, 'company', FILTER_SANITIZE_STRING);
$referral = filter_input(INPUT_POST, 'referral', FILTER_SANITIZE_STRING);
$phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING);
// Do validate
validate($name, $email);
// If validate success
$body = " From: $name\n\n E-Mail: $email\n\n Address: $company\n\n Phone: $phone\n\n Referral: $referral\n\n";
// Send email
$to = $config['to'];
$subject = $config['subject'];
// From and Reply-To are the same
$from = sprintf( '%1$s <%2$s>', $name, $email);
$headers = array();
$headers[] = sprintf('From: %s', $from);
$headers[] = sprintf('Reply-To: %s', $from);
$headers[] = sprintf('X-Mailer: PHP/%s', phpversion());
$headers[] = 'MIME-Version: 1.0';
// Convert headers from array to string
$headers = implode("\r\n", $headers);
mail($to, $subject, $body, $headers);
send_json(); ?>
这个.js也是邮件的一部分:
(function () {
/**
* Element, where ajax response will be printed.
* @type {*|jQuery|HTMLElement}
*/
var responseHolder = $('.response-holder');
function mailer_response_success(data) {
var data = data || 'You have submitted an estimate for review. Thank you!';
responseHolder.addClass('success').html('<div class="inner">' + data + '</div>');
}
function mailer_response_error(data) {
var message = ('string' == typeof data) ? data : 'Sorry, AJAX error occurred';
// If data not a scalar sting
if ('object' == typeof data) {
var a = [];
$.each(data, function(key, value) {
a.push(value);
});
message = a.join('<br>');
}
responseHolder.addClass('error').html('<div class="inner">' + message + '</div>');
}
// Mailer form handler
$(document).on('submit', '#ajax-form', function (e) {
// Stop form submitting
e.preventDefault();
var form = $( this),
formdata = form.serializeArray();
responseHolder.html('');
responseHolder.removeClass('success error');
// Data validation here?
$.ajax({
url: 'mailer/mailer.php',
type: 'POST',
dataType: 'json',
data: formdata,
error: function(xhr, status, error) {
console.log(['kennedy.mailer.error', status, error, xhr.responseText]);
},
success: function(response) {
console.log(response);
if (response.success.length !== 0 && true == response.success) {
mailer_response_success();
} else {
mailer_response_error(response.data);
}
}
});
});
})();