Ajax jQuery联系表单无法正常工作

时间:2014-01-02 13:17:05

标签: php jquery ajax

我的Ajax联系表单有问题,特别是php文件。

Ajax代码:

var dataString = 'name=' + name + '&email=' + email + '&message=' + message + '&submitted=true&isAjax=1';
        $.ajax({
            type: "POST",
            url: $('.form').attr('action'),
            data: dataString,
            success: function (msg) {
                $('.img.ajax-loader').css({ visibility: 'hidden' });

                if (msg == 'Mail sent') {

                    $('.response-output').html(contactFormVars.contact_form_error).removeClass('mail-sent-ng, validation-errors');
                    $('.response-output').html(contactFormVars.contact_form_success_message).addClass('mail-sent-ok');
                } else {
                    $('.response-output').html(contactFormVars.contact_form_error).removeClass('mail-sent-ok, validation-errors');
                    $('.response-output').html(contactFormVars.contact_form_error).addClass('mail-sent-ng');
                    $('.contactForm .submit').removeAttr("disabled");
                }

                $('.contactForm .submit').removeAttr("disabled");
            },
            error: function (ob, errStr) {
                $('.response-output').html(contactFormVars.contact_form_error).removeClass('mail-sent-ok, validation-errors');
                $('.response-output').html(contactFormVars.contact_form_error).addClass('mail-sent-ng');
                //Activate the submit button
                $('.contactForm .submit').removeAttr("disabled");
            }

        });
        $(".ajax-loader").css("visibility", "hidden");
        return false;

PHP:

<? php
if ($_POST) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message']; 
    //send email 
    mail("test@mail.com", "Subject" .$email, $message); 
    return true; 
} 
?>

我制作了一个简单的PHP脚本邮件,电子邮件到达收件人,但我有一个问题,如何返回返回Ajax脚本的值,以显示邮件已发送的消息。

抱歉我的英语不好,我希望我能以清晰的方式描述我的问题。我将非常感谢你的帮助。谢谢。

3 个答案:

答案 0 :(得分:0)

更改PHP脚本

if($_POST){ 
    $name = $_POST['name']; 
    $email = $_POST['email']; 
    $message = $_POST['message']; 
    //send email 
    mail("test@mail.com", "Subject" .$email, $message); 
    echo "Mail sent"; 
}

在JS上添加trim()

if (msg.trim() == 'Mail sent') {

}

答案 1 :(得分:0)

试试这个

$.ajax({
        type: "POST",
        url: $('.form').attr('action'),
        data: dataString,
        success: function (msg) {
            $('.img.ajax-loader').css({ visibility: 'hidden' });
            if (msg == '1') {
               // do what you want
            }
      });

答案 2 :(得分:0)

试试这个:

<?php
$email = $_POST['email'];
$message = $_POST['message'];
$name = $_POST['name'];
$isAjax = isset($_POST['isAjax']) && $_POST['isAjax'] == "1";

$mailSent = dealWithEmail($name, $email, $message);

if ($isAjax) {
    if ($mailSent) {
        echo "Your message has been sent"; // This will be the message received
    } else {
        echo "Your message has NOT been sent"; // This will be the message received
    }
} else {
    // redirect to "email sent (or not)" page
}