Allen.Yu
Allen.Yu

Reputation: 3

Can't stop the spinner when using Bootstrap Ladda UI

I'm new to the web developing, i will try my best to explain this.I'm try to do a contact us form and the code below are pass the information from the form to the PHP file and PHP file will return the result of sending.

So, i'm trying to add in the Ladda UI in the function, so when people click on the send message button, it will start the spinning and it will stop when php file returns the result.I manage to put the Ladda.bind( '.ladda-button'); in the index page so it will start spinning, but i couldn't work out how to stop this, unless i put a time limit on it.

i'm lack the knowledge for reading the code below, please help me/explain how the code works and how should i fix this.! Thanks a lot..

https://github.com/msurguy/ladda-bootstrap

$(function() {
$("input,textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },      
    submitSuccess: function($form, event) {         
        event.preventDefault(); // prevent default submit behaviour     
        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }           
        $.ajax({
            url: "././mail/contact_me.php",
            type: "POST",
            data: {
                name: name,
                phone: phone,
                email: email,
                message: message
            },      
            cache: false,
            success: function() {                   
                // Success message
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');                  
                //clear all fields
                $('#contactForm').trigger("reset");
                $('#contactForm').click()
            },
            error: function() {
                // Fail message
                $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#contactForm').trigger("reset");
            },
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
});
$("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
});

});

html code added:

    <form name="sentMessage" id="contactForm" novalidate>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="form-group">
                                <input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                            <div class="form-group">
                                <input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="col-lg-12 text-center">
                            <div id="success"></div>
                            <button type="submit" id="form-submit"class="btn btn-xl ladda-button" data-style="expand-right" data-color='blue' data-size="l" ><span class="ladda-label">Send Message</span></button>

                        </div>
                    </div>
                </form>

code for loading the animations

    <script>
        // Bind normal buttons
        Ladda.bind( '.ladda-button');
    </script>

Upvotes: 0

Views: 2792

Answers (1)

Mangesh Parte
Mangesh Parte

Reputation: 2177

Try this code:

$(function() {
    var button;
    $('.ladda-button').click(function(e){
        button = this;
    })
    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },      
        submitSuccess: function($form, event) {         
            event.preventDefault(); // prevent default submit behaviour     
            var l = Ladda.create(button);
            l.start();  // Starts the spinner
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }           
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },      
                cache: false,
                success: function() {                   
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                    $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                    .append('</div>'); 
                    l.stop(); // Stops the spinner
                    //clear all fields
                    $('#contactForm').trigger("reset");
                    $('#contactForm').click()
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    l.stop(); // Stops the spinner
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });
    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});

And remove this line 1st

Ladda.bind( '.ladda-button');

Upvotes: 3

Related Questions