Carl
Carl

Reputation: 111

validate contact form with jquery and php

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 = '[email protected]';                            
$mail->Password = 'pass';                          
$mail->SMTPSecure = 'tls';                            

$mail->From = '[email protected]';
$mail->FromName = 'Contact Form';
$mail->AddAddress('[email protected]');               

$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.

Upvotes: 1

Views: 356

Answers (3)

Chinmay235
Chinmay235

Reputation: 3414

Hi put your code between

$(document).ready(function() {

});

Looks like

$(document).ready(function() {
$('#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.");
              }
          });
      }
  });
});

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You have to use echo here, this is not function. return is used for returning result from functions. Also remove the ; after

else {...};
          ^

if(!$mail->Send()) {
    echo false;
} else {
    echo true;    
}

Upvotes: 1

Andy Jones
Andy Jones

Reputation: 6275

In your click function

$('#button_form').click( function() { .... } );

Be sure to return false if validaForm returns false

Upvotes: 0

Related Questions