用jquery和php验证联系表单

时间:2013-07-18 13:48:06

标签: php javascript jquery

我正在尝试使用jquery和php验证联系表单但是我总是从php得到错误,即使它工作正常。我将用我的代码解释:
这是我的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文件中弄错,无论它是否发送电子邮件。
难道我做错了什么??有什么我想念的吗? 我将不胜感激任何帮助。很多。

3 个答案:

答案 0 :(得分:1)

你必须在这里使用echo,这不是功能。 return用于返回函数的结果。同时删除<{p>}之后的;

else {...};
          ^

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

答案 1 :(得分:0)

在您的点击功能

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

如果validaForm返回false,请务必return false

答案 2 :(得分:0)

你好把你的代码放在

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