Writing Ajax Form with IF ELSE statements
I'm writing an Ajax from for a project and I'm having a bit of a problem. When I click the submit button the form still goes to the
sendmail.php page though I have
return false
Can someone take a look at this and tell me where I may have gone wrong... Please?
- $('#MyForm').submit(function(){
var subject = $(".subject").val();
var name = $(".name").val();
var email = $(".email").val();
var message = $(".message").val();
var pattern = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/i; // RegEx pattern
var checkmail = email.search(pattern); // stores value of search -1 if it doesn't match
var checkspace = email.search(/ /g); // find spaces
if(checkmail != -1 && checkspace == -1 && subject != "" && name != "" && message != "")
{
$.get('sendmail.php',
{subject: subject, name: name, email: email, phone: phone, message: message },
function(data){
$('.contact .output').fadeOut('slow').remove();
$('.contact').append('<div class="output"><h3>Thank You! You e-mail has been sent.</h3></div>');
$('.contact .output').hide();
$('.contact .output').fadeIn('slow');
$('.contact .output').fadeOut(6000);
}
);
}
else
{
$('.contact').append('<div class="output"><h3>OOPS! Seems you forgot something...</h3></div>');
if(subject == "")
{
$('.contact .error-1').fadeIn('slow');
}
if(name == "")
{
$('.contact .error-2').fadeIn('slow');
}
if(email == "" || checkmail == -1 || checkspace != -1)
{
$('.contact .error-3').fadeIn('slow');
}
if(message == "")
{
$('.contact .error-4').fadeIn('slow');
}
}
return false;
}
);
As I said the form works... just that I want it to send the data to the sendmail.php with on moving from the contact-us.php.