rakeshkumar
rakeshkumar

Reputation: 91

Javascript modal auto close after message

I am using below java script for contact form submission & after particular time the modal should be closed automatically

if ($('.js-form').length) {
    $('.js-form').each(function() {
        $(this).validate({
            errorClass: 'error form-error',
            submitHandler: function(form) {
                $.ajax({
                    type: "POST",
                    url: "mail.php",
                    data: $(form).serialize(),
                    success: function() {
                        $('#error').modal('hide');
                        $('#success').modal('show');
                    },
                    error: function() {
                        $('#success').modal('hide');
                        $('#error').modal('show');
                    }
                });
            }
        });
    });
}

Upvotes: 0

Views: 88

Answers (3)

Harikrishna Yakkaluri
Harikrishna Yakkaluri

Reputation: 25

To close after a particular time, you have to use setTimeOut. with your current code, the hide/show functionality will execute immediately after the ajax response.

Upvotes: 0

Raul Sauco
Raul Sauco

Reputation: 2705

You could use setTimeout() inside the handlers like this.

success: function() {
    $('#error').modal('hide');
    $('#success').modal('show');

    setTimeout(function(){ 
        $('#success').modal('hide'); 
    }, 3000);
},
error: function() {
    $('#success').modal('hide');
    $('#error').modal('show');

    setTimeout(function(){ 
        $('#error').modal('hide'); 
    }, 3000);
}

Upvotes: 1

ahmednawazbutt
ahmednawazbutt

Reputation: 833

You need to return false at the end of submission of your form.

Upvotes: 1

Related Questions