基本点击功能进入AJAX调用

时间:2015-08-29 03:33:31

标签: javascript php jquery ajax

我正在寻找将基本点击功能转换为AJAX调用,但我不知道如何使用我的php脚本发送的附加响应。

我怎么能这样做?

我现在有以下脚本......

$(document).ready(function(){

    $('#admin_submit').click(function(){

        $.post("admin_help_send.php", $("#admin_help_form").serialize(),  function(response) {
            if (response == 'Your email was sent!') {
                $('#contactMessageStatus').append(response);
                $('#contactMessageStatus').addClass("contactSuccess");
                $('html, body').animate({
                    scrollTop: $("#contactMessageStatus").offset().top
                }, 2000);
                $('#contactMessageStatus').html(response);
                $('#contactMessageStatus').delay(5500).fadeOut(400);
            }
            else {
                $('#contactMessageStatus').append(response);
                $('#contactMessageStatus').addClass("contactFail");
                $('html, body').animate({
                    scrollTop: $("#contactMessageStatus").offset().top
                }, 2000);
                $('#contactMessageStatus').html(response);
                $('#contactMessageStatus').delay(5500).fadeOut(400);
            }
            });
            return false;
        });
    });

它得到了来自php的响应,并附上了响应..

if (!empty($email)) { 
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 

    //Should also do a check on the mail function
    if (mail($to, $subject, $message, $headers)) {
        echo "Your email was sent!"; // success message
    } else {
        echo "Mail could not be sent!"; // failed message
    }

  } else { 
        //Invalid email
        echo "Invalid Email, please provide a valid email address.";
    }

} else {
    echo "Email Address was not filled out.";
}

1 个答案:

答案 0 :(得分:0)

查看可与.ajax()一起使用的不同设置。这应该可以让你到达目的地:

$('#admin_submit').click(function () {
    var sendData = $("#admin_help_form").serialize();
    $.ajax({
        url: "admin_help_send.php",
        data: sendData,
        type: "POST",
        success: function (response) {
            if (response == 'Your email was sent!') {
                $('#contactMessageStatus').append(response);
                $('#contactMessageStatus').addClass("contactSuccess");
                $('html, body').animate({
                    scrollTop: $("#contactMessageStatus").offset().top
                }, 2000);
                $('#contactMessageStatus').html(response);
                $('#contactMessageStatus').delay(5500).fadeOut(400);
            } else {
                $('#contactMessageStatus').append(response);
                $('#contactMessageStatus').addClass("contactFail");
                $('html, body').animate({
                    scrollTop: $("#contactMessageStatus").offset().top
                }, 2000);
                $('#contactMessageStatus').html(response);
                $('#contactMessageStatus').delay(5500).fadeOut(400);
            }
        },
        error: function (data) {
            // handel errors here
        }
    });
    return false;
});