首先,我应该说我对 php 一无所知,我正在尝试自己修改我网站中的某些内容。我希望用户在提交联系表格后被重定向到感谢页面。我一直在通过所有其他答案阅读有关此主题的内容,但我仍然不明白。
我的html代码是:
<div id="form_container">
<div id="main">
<p><small>Name</small> <input type="text" name="name" id="name" /></p>
<p><small>Email</small> <input type="text" name="email" id="email" /></p>
<p><small>Phone</small> <input type="text" name="phone" id="phone" /></p>
<p><small>Message</small> <textarea name="message" id="message" rows="12"></textarea></p>
<p><input type="submit" name="submit" id="submit" value="Email Us!" /></p>
<ul id="response" />
</div><!--end main -->
我的PHP代码是:
<?php
$name = trim($_POST['name']);
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = stripslashes($_POST['message'] . "<br>Phone Number: " . $_POST['phone']);
$site_owners_email = 'xxxxxxx'; // Replace this with your own email address
$site_owners_name = 'xxxxxxxxx'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Your name is.....";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Invalid email address";
}
if (strlen($phone) < 10) {
$error['phone'] = "Your phone number?";
}
if (strlen($message) < 5) {
$error['message'] = "Your message?";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Website Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->AddAddress('xxxxxxxxxx', 'xxxxxxxx');
$mail->Body = $message
$mail->Send();
echo "<li class='success'> Congratulations, " . $name . ". We received your message</li>";
} # end if no error
else {
$response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
$response .= (isset($error['phone'])) ? "<li>" . $error['phone'] . "</li> \n" : null;
$response .= (isset($error['message'])) ? "<li>" . $error['message'] . "</li>" : null;
echo $response;
} # end if there was an error sending
?>
我的 js 文件是
$(function() {
var paraTag = $('input#submit').parent('p');
$(paraTag).children('input').remove();
$(paraTag).append('<input type="button" name="submit" id="submit" value="Email Us Now!" />');
$('#main input#submit').click(function() {
$('#main').append('<img src="img/ajax-loader.gif" class="loaderIcon" alt="Loading..." />');
var name = $('input#name').val();
var email = $('input#email').val();
var phone = $('input#phone').val();
var message = $('textarea#message').val();
$.ajax({
type: 'post',
url: 'sendEmail.php',
data: 'name=' + name + '&email=' + email + '&phone=' = phone + '&comments=' + comments,
success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
$('#response').html(results);
}
}); // end ajax
});
});
我只想让用户在填写表格后看到一个简单的感谢页面。