Search code examples
javascriptjqueryformstwitter-bootstrapbootstrapvalidator

BootstrapValidator not showing success message


Edit: The original codepen I borrowed this from is here. The only difference is in the HTML I changed the action=' ' (it was blank, all php code was in the same file as the form) to action='mail-form.php'. Is this why the javascript isn't working?

I'm using bootstrapvalidator for validation on my php form. It works great but when I submit the form, instead of showing my success notification it is going to the form's action page. The form works and the email is sent.

Here is my form declaration:

<form class="well form-horizontal" action="mail-form.php" method="post"  id="contact_form">

Here is the bootstrap validator's call to submit the form in my javascript:

  $(document).ready(function() {
    $('#contact_form').bootstrapValidator({
        // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // Removed validation rules for brevity //
       .on('success.form.bv', function(e) {
            $('#success_message').slideDown({ opacity: "show" }, "slow") // Do something ...
                $('#contact_form').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');
        });
});

When I submit the form after validation is successful, it directs me to the form's action which is mail-form.php. I was expecting it to just show the #success-message div as stated above in the javascript, but it does not. Just opens mail-form.php which is a blank page that echoes out the fields values that were submitted (for my debugging).

If it helps, here's where I'm adding my script files in my pages footer.

  ...
  </footer>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <?php if (basename($_SERVER['PHP_SELF']) == "contact.php") { ?>
      <script src="https://www.google.com/recaptcha/api.js"></script>
    <?php } ?>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-validator/0.4.5/js/bootstrapvalidator.min.js"></script>
    <script src="js/scripts.js"></script>
</body>

I was always told any script declarations needed to be at the end of the file so they get loaded right before the page is completely loaded. I don't know if this is a reason for the problem I'm having (I'm very new to jquery)


Solution

  • Add this submit event to prevent the form from submitting on click

    $('#contact_form').on('submit',function(e){
    e.preventDefault();
    });
    

    or use the function:

    submitHandler: function(validator, form, submitButton) {}
    

    see:http://codepen.io/anon/pen/MadxaE?editors=001