Nathan
Nathan

Reputation: 499

Closing Modal when using ajax and validation

i have run into a little issue for closing a data modal with bootstrap i have showed a bit of code below basically at the current stage the ajax validates the form via the input submit field this works however it doesn't close the modal it resides in

so my question how can i utilize the same ajax code but allowing the data modal to close on submission please bare in mind the modals id is webapp_customers_modal but is dynamic with a number on the end as its used in multiple places

this is the preferred method i would like to use but doesn't submit the form

<button type='submit' data-dismiss='modal'data dismiss='modal'>Save</button>

This is the current form submit im using this works but doesn't close the modal

<input type='submit' class='btn btn-info' value='Save' >

this is the ajax code

        <script>
        $(function() {
            $('form.frm_details').on('submit', function(event) {
                event.preventDefault();
                $.ajax({
                    url: '/limitless/functions2.php',
                    type: 'post',
                    dataType: 'json',                       
                    data: $(this).serialize(),
                      success: function(data) {
                           if(data.status == '1')
                            {
                                $('#info').addClass('alert alert-danger no-border').html(data.message);
                            } 
                           if(data.status == '2')
                            {
                                $('#info').addClass('alert alert-danger no-border').html(data.message);
                                webapp_get_customers(); 
                            }                               
                        }  
                });
            });
        });
    </script>

Upvotes: 0

Views: 269

Answers (2)

Andy
Andy

Reputation: 397

If you only have one of your modals open at a time you can add a common class, like my-modals to them and then use this in you script. This way you don't need to keep track of your dynamic created id's.

$('.my-modals').modal('hide');

Upvotes: 3

WhatisSober
WhatisSober

Reputation: 988

Why not close modal using javascript? Give your modal an Id

$('#myModal').modal('hide'); //This closes modal with id 'myModal'

PS: you can add the above code inside success callback.

Upvotes: 0

Related Questions