在应用验证 jquery 后按 post 提交数据不起作用($_POST 为空)

submit data per post after validation jquery applied doesn't work ($_POST empty)

我在 Jquery、AJAX 和字段验证方面遇到问题。

如果输入一切正常,验证有效,重定向有效,但我的字段值不包含在 $_POST 数组中。

这是表单开始标记:

<form class="smart-form" id="register-form" method="post" action="#pages/target.php" novalidatee>

这里是JavaScript部分:

<script type="text/javascript">              
        // Load form valisation dependency 
loadScript("js/plugin/jquery-form/jquery-form.min.js", runFormValidation);


// Registration validation script
function runFormValidation() {
    //e.preventDefault();
    var $checkoutForm = $('#register-form').validate({

    // Rules for form validation
        rules : {
            name : {
                required : true
            },
            vorname : {
                required : true
            },  
            passwort : {
                required : true,
                minlength: 8
            },
            passwort2 : {
                required : true,
                minlength: 8
            },
            email : {
                required : true,
                email : true
            },          
            admin : {
                required : true,
                min: {
                  // min needs a parameter passed to it
                  param: 1
                }
            },
        },

        // Messages for form validation
        messages : {
                /* My Messagees*/ not mentionend

        },
        // Ajax form submition
        success : function() {
                     form.submit();
                    $('#register-form').addClass('submited');
                },


        // Do not change code below
        errorPlacement : function(error, element) {
            error.insertAfter(element.parent());
        }
    });        
}; 

阅读该插件的 documentation for the validate method,我发现当您真正想要提交有效表单时,您应该使用 submitHandler 回调。

submitHandler (default: native form submit):

Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit. The right place to submit a form via Ajax after it is validated.

正如@Sparky 提到的: 除非您想在有效元素上使用错误标签,例如绿色复选标记或其他东西,否则您通常不需要成功选项。

success Type: String or Function()

If specified, the error label is displayed to show a valid element. If a String is given, it is added as a class to the label. If a Function is given, it is called with the label (as a jQuery object) and the validated input (as a DOM element). The label can be used to add a text like "ok!".

因此,不应将 form.submit(); 位放在 success 回调中,而应将其放在 submitHandler 中。

因此您的代码应如下所示:

// Optional, read what this does
success: 'valid', 

// This does the actual form submitting
submitHandler: function (form) {
    form.submit();
},

顺便说一句,form.submit()不是AJAX,据我了解,它只是以传统方式提交表单。