Sorry for bashing this one to death yet I want to implement a simple AJAX/PHP contact form and I have it nearly working except for the PHP validation.
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'XXX.net';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$headers = 'From: XXX.net' . "\\r\
";
//if no message entered and no email entered print an error
if (empty($message) && empty($email)){
echo "No email address and no message was entered. <br>Please include an email and a message";
}
//if no message entered send print an error
elseif (empty($message)){
echo "No message was entered.<br>Please include a message.<br>";
}
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
}else{
echo "Invalid email! <br/>Please provide a correct email address for us to contact you.";
}
?>
The email invalid error works yet the
if (empty($message) && empty($email))
does not… Sure its a syntax problem.
The AJAX for reference is;
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
$.post("contact/send.php", $("#mycontactform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
</script>