duan5362 2013-07-18 13:48
浏览 42
已采纳

用jquery和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 = '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;    
};

?> 

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.

  • 写回答

3条回答 默认 最新

  • download2014711 2013-07-18 13:50
    关注

    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;    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀