I'm trying to validate a contact form with jquery and php but I always get false from php even though it's working fine. I'm going to explain with my code:
This is my html:
<form method='post'>
<label>Name<input id="name" type="text" name="name" /></label>
<label>Email Address<input id="email" type="text" name="email" /></label>
<label>Subject<input id="subject" type="text" name="subject" /></label>
<label>Message<textarea id="message" name="message"></textarea></label>
<label><input type="button" value="Submit Form" id="button_form" /></label>
</form>
This is my js code:
function validaForm(){
// Campos de texto
if($('#email').val() == ""){
alert("You must fill in at least the email field.");
$('#email').focus();
return false;
}
return true;
}
$('#button_form').click( function() {
if(validaForm()){
$.post("php/form.php",$('form').serialize(),function(res){
if(res == 1){
alert("Your e-mail has been sent, Thank you for contacting us. We will answer you as soon as possible.");
} else if(res == 0){
alert("Sorry, your e-mail couldn't been sent, please try again later.");
} else {
alert("Others.");
}
});
}
});
This is my .php file:
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'smtp.domain.com';
$mail->SMTPAuth = true;
$mail->Username = 'myacc@mydomain.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->From = 'myacc@mydomain.com';
$mail->FromName = 'Contact Form';
$mail->AddAddress('myacc@mydomain.com');
$mail->AddAttachment('/var/tmp/file.tar.gz');
$mail->AddAttachment('/tmp/image.jpg', 'new.jpg');
$mail->IsHTML(true);
$mail->Subject = $_POST['subject'];
$mensajeFinal = "Message";
$mail->Body = $mensajeFinal;
if(!$mail->Send()) {
return false;
} else {
return true;
};
?>
Ok, Everything is working fine, I mean, it sends the e-mail but the problem is that I always get false from php file no matter if it sends or not the e-mail.
Am I doing something wrong?? Is there something I am missing??
I would appreciate any help. Thanxs a lot.