ajaxSubmit.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Using Ajax to submit data</title>
  5. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  6. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  7. <script type="text/javascript" src="../vendor/jquery/jquery.min.js"></script>
  8. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  9. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  10. </head>
  11. <body>
  12. <div class="container">
  13. <div class="row">
  14. <div class="col-lg-8 col-lg-offset-2">
  15. <div class="page-header">
  16. <h2>Using Ajax to submit data</h2>
  17. </div>
  18. <form id="defaultForm" method="post" class="form-horizontal" action="ajaxSubmit.php">
  19. <div class="form-group">
  20. <label class="col-lg-3 control-label">Username</label>
  21. <div class="col-lg-5">
  22. <input type="text" class="form-control" name="username" />
  23. </div>
  24. </div>
  25. <div class="form-group">
  26. <label class="col-lg-3 control-label">Email address</label>
  27. <div class="col-lg-5">
  28. <input type="text" class="form-control" name="email" />
  29. </div>
  30. </div>
  31. <div class="form-group">
  32. <label class="col-lg-3 control-label">Password</label>
  33. <div class="col-lg-5">
  34. <input type="password" class="form-control" name="password" />
  35. </div>
  36. </div>
  37. <div class="form-group">
  38. <div class="col-lg-9 col-lg-offset-3">
  39. <button type="submit" class="btn btn-primary">Sign up</button>
  40. </div>
  41. </div>
  42. </form>
  43. </div>
  44. </div>
  45. </div>
  46. <script type="text/javascript">
  47. $(document).ready(function() {
  48. $('#defaultForm')
  49. .bootstrapValidator({
  50. message: 'This value is not valid',
  51. feedbackIcons: {
  52. valid: 'glyphicon glyphicon-ok',
  53. invalid: 'glyphicon glyphicon-remove',
  54. validating: 'glyphicon glyphicon-refresh'
  55. },
  56. fields: {
  57. username: {
  58. message: 'The username is not valid',
  59. validators: {
  60. notEmpty: {
  61. message: 'The username is required and can\'t be empty'
  62. },
  63. stringLength: {
  64. min: 6,
  65. max: 30,
  66. message: 'The username must be more than 6 and less than 30 characters long'
  67. },
  68. /*remote: {
  69. url: 'remote.php',
  70. message: 'The username is not available'
  71. },*/
  72. regexp: {
  73. regexp: /^[a-zA-Z0-9_\.]+$/,
  74. message: 'The username can only consist of alphabetical, number, dot and underscore'
  75. }
  76. }
  77. },
  78. email: {
  79. validators: {
  80. notEmpty: {
  81. message: 'The email address is required and can\'t be empty'
  82. },
  83. emailAddress: {
  84. message: 'The input is not a valid email address'
  85. }
  86. }
  87. },
  88. password: {
  89. validators: {
  90. notEmpty: {
  91. message: 'The password is required and can\'t be empty'
  92. }
  93. }
  94. }
  95. }
  96. })
  97. .on('success.form.bv', function(e) {
  98. // Prevent form submission
  99. e.preventDefault();
  100. // Get the form instance
  101. var $form = $(e.target);
  102. // Get the BootstrapValidator instance
  103. var bv = $form.data('bootstrapValidator');
  104. // Use Ajax to submit form data
  105. $.post($form.attr('action'), $form.serialize(), function(result) {
  106. console.log(result);
  107. }, 'json');
  108. });
  109. });
  110. </script>
  111. </body>
  112. </html>