On the front end everything appears to be functioning; the 'Your message has been successfully sent!' appears when the submit button is pressed on the contact section (located on the page index.php - the only page on the website). Nevertheless, when I check the email address the php form is supposed to have been sent to, there is nothing received.... Can anyone suggest why?
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"]."
";
$mailheader .= "Reply-To: ".$_POST["email"]."
";
$mailheader .= "Content-type: text/html; charset=iso-8859-1
";
$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");
}
?>