提交表单时AJAX无法正常工作

时间:2017-08-04 09:35:10

标签: php ajax

我不知道出了什么问题。我的验证效果很好,但是当我按下按钮执行AJAX调用时,它无法使用POST执行“正常方式”。

这是我的代码:

$(document).ready(function () { 
    $("#loginSubmit").validate({
        errorElement: "div",
        errorPlacement: function(error, element) {
            error.appendTo("div#errors");
        },
        rules: {
            "username": {
                required: true,
                minlength: 5

            },  
            "password": {
                required: true,
                minlenght: 5,
                maxlenght: 20

            }
        },
        messages: {
            "username": {
                required: "<div class='alert alert-danger'> Wprowadź poprawną nazwę użytkownika </div>",
                minlength: "<div class='alert alert-danger'> Wymagane minumum 3 znaki </div>"
            },  
            "password": {
                required: "<div class='alert alert-danger'> Wprowadź hasło </div>",
                minlength: "<div class='alert alert-danger'> Wymagane minimum 8 znaków</div>",
                maxlength: "<div class='alert alert-danger'> Wymagane maksymalnie 15 znaków</div>"
            }
        },
      submitHandler: submitForm
    });

function submitForm()
{
    var data = $("#loginSubmit").serialize();

    $.ajax({

        type : 'POST',
        url  : 'login.php',
        data : data, 
        beforeSend: function()
        {
            $("#error").fadeOut();
            $("#login-Submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; logowanie...');
        },
        success :  function(data)
        {
            if(data==1){

                $("#error").fadeIn(1000, function(){


                    $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry email already taken !</div>');

                    $("#login-Submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                });

            }
            else if(data=="registered")
            {

                $("#login-Submit").html('Signing Up');
                setTimeout('$(".card-block").fadeOut(500, function(){ $(".card-block").load("ADMIN/index.php"); }); ',5000);

            }
            else{

                $("#error").fadeIn(1000, function(){

                    $("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+data+' !</div>');

                    $("#login-Submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                });


            }
        }
    });
    return false;
}
});

当我删除验证代码并刷新页面时,AJAX请求启动但没有结果。我不知道为什么它不能通过按下按钮开始运作。

0 个答案:

没有答案