1

我想知道是否有一种方法可以使用 Bootstrap Validator 将我的所有输入字段值发送到远程 PHP 文件。

为了解释,我在登录表单中有两个输入字段。我在两个输入字段上都使用了 Bootstrap Validator 的远程验证。$_POST每个验证只发送请求的输入字段的值,并且无法通过PHP 后端访问其他输入字段。

我知道我可以使用 data 选项将数据发送到 PHP 后端,但我不确定如果我要在该部分中发送不同输入字段的值,格式会如何进行。

这是一个编码示例。

$(document).ready(function() {
    $('#login_form').bootstrapValidator({
        fields: {
            username: {
                message: 'Invalid',
                validators: {
                    remote: {
                        message: 'Invalid',
                        url: '/path/to/backend/',
                        data: {
                            password: // What do I put here?
                        }
                    }
                }
            },
            password: {
                message: 'Invalid',
                validators: {
                    remote: {
                        message: 'Invalid',
                        url: '/path/to/backend/',
                        data: {
                            username: // What do I put here?
                        }
                    }
                }
            }
        }
    });
});

该编码示例是解决我的问题并将两个输入字段值提交到后端的一种方法。如果有人用这种方法解决我的问题,或者我可以将所有输入字段值提供给后端的另一种方法,请告诉我。

预先感谢您提供的任何帮助。

4

2 回答 2

0

要将表单中的任何输入字段发送到后端,只需调用一个验证器函数,该函数会调用表单中输入字段的值并将它们分配给具有描述性名称的参数。然后可以从 /path/to/backend 中的 POST(或从 GET 我这样配置它)访问此参数。

您可以在下面看到您修改的代码。我希望它对你有用。它是经过全面测试的代码。

请发送反馈。

$(document).ready(function() {
$('#login_form').bootstrapValidator({
    fields: {
        username: {
            message: 'Invalid',
            validators: {
                remote: {
                    url: '/path/to/backend/',
                    data: function(validator) {
                       return {
                           password: $('[name="passwordNameAttributeInYourForm"]').val(),
                           whatever: $('[name="whateverNameAttributeInYourForm"]').val()
                       };
                    },
                   message: 'Invalid'
                }
            }
        },
        password: {
            message: 'Invalid',
            validators: {
                remote: {
                    url: '/path/to/backend/',
                    data: function(validator) {
                       return {
                           username: $('[name="usernameNameAttributeInYourForm"]').val(),
                           whatever: $('[name="whateverNameAttributeInYourForm"]').val()
                       };
                    },
                   message: 'Invalid'
                }
            }
        }
    }
});

});

于 2014-12-29T12:11:33.030 回答
0

要使用引导验证器将数据发送到后端,请使用此代码。

$(document).ready(function() {
          $('#student_view').bootstrapValidator({
              // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
              container: 'tooltip',
              feedbackIcons: {
                  valid: 'glyphicon glyphicon-ok',
                  invalid: 'glyphicon glyphicon-remove',
                  validating: 'glyphicon glyphicon-refresh'
              },
              fields: {
                  coursename: {
                      validators: {
                          notEmpty: {
                              message: 'Please select course name'
                          },
                          remote: {
                            url: 'abc.php', //path to backend
                            data: function(validator) {
                               return {
                                   course: $('#course').val(), //sending dynamic data value
                                   password: $('#password').val()
                               };
                            },
                           message: 'Invalid'
                        }
                      }
                  },


                 }
              })
              .on('success.form.bv', function(e) {
                      $('#student_view').data('bootstrapValidator').resetForm();

                  // Prevent form submission
                  e.preventDefault();

                  // Get the form instance
                  var $form = $(e.target);

                  // Get the BootstrapValidator instance
                  var bv = $form.data('bootstrapValidator');

                  // Use Ajax to submit form data
                  $.post($form.attr('action'), $form.serialize(), function(result) {
                      console.log(result);
                  }, 'json');
              });
      });

后端必须以以下格式返回输出

{ "valid": true } or { "valid": false }
于 2019-05-20T08:21:34.607 回答