使用jquery和php验证联系人表单


validate contact form with jquery and php

我试图用jquery和php验证联系人表单,但我总是从php得到false,即使它运行良好。我将用我的代码进行解释:
这是我的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> 

这是我的js代码:

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.");
                }
            });
        }
    }); 

这是我的.php文件:

<?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;    
};
?> 

好的,一切都很好,我的意思是,它发送电子邮件,但问题是,无论它是否发送电子邮件,我总是从php文件中得到false
我做错什么了吗??我缺了什么吗
如果有任何帮助,我将不胜感激。非常感谢。

这里必须使用echo,这不是函数。return用于返回函数的结果。同时移除后的;

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

在您的点击功能中

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

如果validaForm返回错误,请确保使用return false

嗨,把你的代码放在之间

$(document).ready(function() {
});

看起来像

$(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.");
              }
          });
      }
  });
});