在前端,一切似乎都在运作; '您的邮件已成功发送!'在联系人部分按下提交按钮时出现(位于页面index.php上 - 网站上唯一的页面)。然而,当我检查电子邮件地址时,应该发送php表单,没有收到任何信息....有人可以提出建议吗?
HTML:
<form id="validationForm" method="post" action="index.php">
<label for="anti" id="antiLabel">This is an anti 's'-to-the-'pam' field. Accordingly, do not delete or enter any information here.</label>
<input name="anti" id="antiInput"/>
<label for="name" class="label"></label>
<input name="name" id="name" class="input" placeholder="Name..." required/>
<label for="email" class="label"></label>
<input name="email" id="email" class="input" placeholder="Email Address..." required/>
<label for="subject" class="label"></label>
<input name="subject" id="subject" class="input" placeholder="Subject..." required/>
<label for="message" id="labelMessage"></label>
<textarea name="message" id="inputMessage" placeholder="Message..." required></textarea>
<input id="submitButton" type="submit" value="Submit" class="input"/>
</form>
jQuery的:
<script>
$("#validationForm").submit(function(event){
var errorMessage="";
event.preventDefault();
/* Regex validation of email address */
if (!isValidEmailAddress($("#email").val())) {
errorMessage="*Please enter a valid email address*";}
if (errorMessage=="") {
$("#error").html("");
// send email via ajax
$.post('index.php', {
name: $('#validationForm').find('input[name=name]').val(),
email: $('#validationForm').find('input[name=email]').val(),
subject: $('#validationForm').find('input[name=subject]').val(),
message: $('#validationForm').find('textarea[name=message]').val()
}, function() {
// after the ajax response and email sent alert the user
alert("Your message has been successfully sent!");
});
} else {
$("#error").html(errorMessage);
}
});
/* '!' concerned with false i.e. if not a valid email */
function isValidEmailAddress(emailAddress) {
var pattern = new
RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
function ValidateEmailOnFly() {
if (isValidEmailAddress($("#email").val()) || !$('#validationForm').find('input[name=email]').val()) {
$("#error").html('');
}
else {
$("#error").html('*Please enter a valid email address*');
}
}
$(document).ready( function() {
$('#validationForm').find('input[name=email]').blur( ValidateEmailOnFly );
});
PHP:
<?php
// check if the form was submitted and only then send the email
if(!empty($_POST["name"])) {
$ToEmail = '123@abc.co.uk';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />";
$MESSAGE_BODY .= "Subject: ".nl2br($_POST["subject"])."<br />";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
}
?>