I have the following code that works great except for one small thing. If an invalid email is entered the else statement of the php comes into the response and with my current styling class for the response, it looks like it was approved.
So, I was wondering if there is anywhere I can create an else statement with my current code and pull the else response from php. If so, how could I do it?
<div id="contactMessageStatus"></div>
<div class="contactForm">
<form action="" method="post" id="mycontactform" >
<input type="text" class="inputbar" name="name" placeholder="Full Name" required>
<input type="email" class="inputbaremail" name="email" placeholder="Email" required>
<textarea rows="4" cols="50" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
<input type="button" class="contactButton" value="Send Message" id="submit">
</form>
</div>
AJAX
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("contactSend.php", $("#mycontactform").serialize(), function(response) {
$('#contactMessageStatus').append(response);
$('#contactMessageStatus').addClass("contactSuccess");
$('html, body').animate({
scrollTop: $("#contactMessageStatus").offset().top
}, 2000);
$('#contactMessageStatus').html(response);
$('#contactMessageStatus').delay(5500).fadeOut(400);
});
return false;
});
});
</script>
PHP file to show the echoed responces:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'contact@example.com';
$subject = 'SFL Contact Form Submitted';
$message = 'FROM: '.$name. ' ' . ' Email: '.$email. ' ' . 'Message: '.$message;
$headers = 'From: contact@example.com' . "
";
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, please provide an correct email.";
}
?>