bootstrap模态注册表单验证并提交

时间:2016-01-12 08:34:00

标签: javascript php jquery forms twitter-bootstrap

我正在尝试验证引导模式注册表单并提交到PHP页面。下面是我的jquery代码。我是jquery的新手,所以无法成功

<script>
    $(function () {
//twitter bootstrap script
        $("button#submit").click(function () {
            $(document).ready(function () {
                $('#register-form').formValidation({
                    framework: 'bootstrap',
                    excluded: ':disabled',
                    icon: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        rfullname: {
                            validators: {
                                notEmpty: {
                                    message: 'The username is required'
                                }
                            }
                        },
                        rpassword: {
                            validators: {
                                notEmpty: {
                                    message: 'The password is required'
                                }
                            }
                        }
                    }
                });
            });

            $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form#register-form').serialize(),
                success: function (msg) {
                    //$("#thanks").html(msg)
                    $("#login-modal").modal('hide');
                    bootbox.alert(msg);
                    $('#register-form').each(function () {
                        this.reset();
                    });
                },
                error: function () {
                    alert("failure");
                }
            });
        });
    });
</script>

并且注册表格是

<form id="register-form" style="display:none;" data-async  method="post" role="form">
   <div class="modal-body">
      <input id="rfullname" class="form-control" name="mname" type="text" placeholder="Your Full Name">
      <input id="remail" class="form-control" name="email" type="text" placeholder="E-Mail">
      <input id="rpassword" class="form-control" name="password" type="password" placeholder="Password">
   </div>
   <div class="modal-footer">
      <div>
         <button type="submit" name="submit" id="submit" class="btn btn-primary btn-lg btn-block">Register</button>
      </div>
      <div>
         <button id="register_login_btn" type="button" class="btn btn-link">Log In</button>
         <button id="register_lost_btn" type="button" class="btn btn-link">Lost Password?</button>
      </div>
   </div>
</form>

此表单应在验证后提交值。

2 个答案:

答案 0 :(得分:0)

试试这个。

  <script>
            $(document).ready(function() {
                     $('#register-form').formValidation({
                        framework: 'bootstrap',
                        excluded: ':disabled',
                        icon: {
                            valid: 'glyphicon glyphicon-ok',
                            invalid: 'glyphicon glyphicon-remove',
                            validating: 'glyphicon glyphicon-refresh'
                        },
                    fields: {
                        rfullname: {
                            validators: {
                                notEmpty: {
                                    message: 'The username is required'
                                }
                            }
                        },
                        rpassword: {
                            validators: {
                                notEmpty: {
                                    message: 'The password is required'
                                }
                            }
                        }
                    },
                    submitHandler : function(validator, form, submitButton) {
                                        saveData();
                                    }
                });
             });

               function saveData(){
                        $.ajax({
                            type: "POST",
                        url: "process.php",
                        data: $('form#register-form').serialize(),
                            success: function(msg){
                                  //$("#thanks").html(msg)
                                $("#login-modal").modal('hide');
                                bootbox.alert(msg);
                                $( '#register-form' ).each(function(){
                                 this.reset();
                                 });    
                            },
                        error: function(){
                            alert("failure");
                            }
                            });
              }
    </script>

答案 1 :(得分:0)

我找到了回答我的问题

<script>
$(function() {

  $('form[name="registor"]').find('input,select,textarea').not('[type=submit]').jqBootstrapValidation({

    submitSuccess: function ($form, event) {

      $.ajax({
                type: "POST",
            url: "process.php",
            data: $('form#register-form').serialize(),
                success: function(msg){
          // just to confirm ajax submission
          $("#login-modal").modal('hide');
                    bootbox.alert(msg);
                    $( '#register-form' ).each(function(){
                     this.reset();
                     });},
            error: function(){
                alert("failure");
                }
                }); 

      // will not trigger the default submission in favor of the ajax function
      event.preventDefault();
    }

  });

});
</script> 

谢谢所有试图帮助我的人。