1

我正在尝试使用 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 文件中得到错误,无论它是否发送电子邮件。
难道我做错了什么??有什么我想念的吗?
我将不胜感激任何帮助。非常感谢。

4

3 回答 3

1

你必须在echo这里使用,这不是功能。return用于从函数返回结果。也去掉;后面

else {...};
          ^

if(!$mail->Send()) {
    echo false;
} else {
    echo true;    
}
于 2013-07-18T13:50:35.503 回答
0

在您的点击功能中

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

确保return false如果validaForm返回false

于 2013-07-18T13:50:27.283 回答
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.");
              }
          });
      }
  });
});
于 2013-07-18T14:03:40.827 回答