繁体   English   中英

使用jquery和php验证联系表单

[英]validate contact form with jquery and php

我正在尝试使用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文件中获取错误信息。
难道我做错了什么?? 有什么我想念的吗?
我将不胜感激任何帮助。 非常感谢。

您必须在这里使用echo ,这不起作用。 return用于从函数返回结果。 也删除;

else {...};
          ^

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

在您的点击功能中

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

时一定要return false如果validaForm返回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.");
              }
          });
      }
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM