submitHandler表单中的Ajax提交不起作用

时间:2015-06-25 07:51:52

标签: php jquery html ajax jquery-validate

我正在注册用户,

我使用jquery验证插件验证字段。

在'submitHandler'中我调用了Ajax然后如果响应正常则表单被提交....但是表单没有被提交。但我得到了“OK”的警告。

这是我的代码,

jQuery(document).ready(function(){
// Setup form validation on the #register-form element
jQuery("#reg_form").validate({
// Specify the validation rules
errorElement: "span",
rules: {
    fname:  {
    required: true,
    maxlength: 12,
    minlength: 4,
    remote: {url: "<?php echo get_template_directory_uri() ?>/ajax-checkusername.php", type : "post"}
    },
    lname: "required",
    phone: {required:true,number: true},
    address: "required",
    email: {
    email: true,
    required: true,
    remote: {url: "<?php echo get_template_directory_uri() ?>/ajax-checkemail.php", type : "post"}
    },
    password: "required",
    cpassword: {
    required: true,
    equalTo: "#password"
    },
    profile_picture: {
    accept: "image/*"
    }
},
// Specify the validation error messages
messages: {
    fname:  {
    email: "This field is required.",
    minlength: "Please enter atleast 4 characters",
    maxlength: "Too much characters",
    remote: "username already in use!",
    },
    lname: "This field is required.",
    phone: {
    required: "This field is required",
    number: "Please Enter Number"
    },
    address: "This field is required.",
    email: {
    required: "Please Enter Email!",
    email: "This is not a valid email!",
    remote: "Email already in use!",
    },
    password: "This field is required.",
    cpassword :{
    required:"This field is required.",
    equalTo:"Password Not matched."
    },
    profile_picture: {
    accept:"Please Insert Image"
    }
},
submitHandler: function(form) { 
    var frm = $(form);alert(frm);

    if (jQuery("#g-recaptcha-response").val()) {

        jQuery.ajax({
            type: 'POST',
            url: "<?php echo get_template_directory_uri(); ?>/ajax_verify_captcha.php", // The file we're making the request to
            dataType: 'html',
            async: true,
            data: {
                captchaResponse: jQuery("#g-recaptcha-response").val()
            },
            success: function (data) {
                alert(data); 

                //jQuery('#reg_form').get(0).submit();
                if(data=="OK"){
                    frm.submit();

                }

                //alert("everything looks ok");
              // jQuery('#reg_form')[0].submit(); 

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("You're a bot");
                alert(XMLHttpRequest);
                return false;
            }

        });


    } else {
        alert("Please fill the captcha!");
        return false;
    }


}
});
});

1 个答案:

答案 0 :(得分:0)

我要检查的第一件事是if语句中的值。此外,您可以使用(数据)查看它是否完全定义或.length以查看它是否已设置为任何内容。

console.log(data=="OK");
if(data.length){
  frm.submit();
}

你应该返回一个布尔值或一个数字并使用parseInt()来确保比较同一数据类型的两个变量。