Close modal bootstrap automatically when send form

Asked

Viewed 20,580 times

4

I have a bootstrap modal whose same has a contact form:

HTML:

Thank you! Your form has been sent successfully and as soon as possible will be answered.

<div class="modal fade" id="contactModal" tabindex="-1" role="dialog" aria-labelledby="contactModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="contactModalLabel">Formulário para contato</h4>
                        </div>
                        <div class="modal-body">

                            <form method="POST" id="ajax_form" action="sendForm.php">
                              <div class="form-group">
                                <label class="control-label">Seu nome:</label>
                                <input type="text" class="form-control" name="name" placeholder="Escreva seu nome">
                              </div>
                              <div class="form-group">
                                <label class="control-label">Seu email:</label>
                                <input type="email" class="form-control" name="email" placeholder="[email protected]">
                              </div>
                              <div class="form-group">
                                <label class="control-label">Assunto:</label>
                                <input type="text" class="form-control" name="subject" placeholder="Escreva o assunto da mensagem">
                              </div>
                              <div class="form-group">
                                <label class="control-label">Mensagem:</label>
                                <textarea class="form-control" id="mensagem" name="message" placeholder="Escreva sua mensagem"></textarea>
                              </div>

                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                            <input type="submit" class="btn btn-primary" value="Enviar mensagem">
                             </form>

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

JS:

$('#ajax_form').submit(function(){
      var dados = $( this ).serialize();

      jQuery.ajax({
        type: "POST",
        url: "sendForm.php",
        data: dados,
        success: function(data)
        {
          $("#msg").attr({"style":"display: block;"});
        }
      });

      return false;
    });

  $("#closeModal").on("click", function(){

   $("#msg").attr({"style":"display: none;"});

So far everything is right, the modal opens correctly, the form is displayed and when I send the form, the thank you alert appears on the screen. However, I would like a function that, once I send the form, the modal automatically closes, because the alert appears but is still in the background, with the modal being highlighted...

Taking advantage of the post, I would also like to ask why the alert take (about 10 seconds) to appear on the screen. Is it because I am using ajax on localhost?

3 answers

8


You can use the modal('hide').

$('#seu_modal').modal('hide');

This can be performed within the success.

Example:

$('.abrir').on('click', function(){

  $('.modal').modal('show');  
  
});

$('.fechar').on('click', function(){

  $('.modal').modal('hide'); 

});

$('form').on('submit', function(){

  $('.modal').modal('hide'); 

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script><button class="abrir"> Abrir </button><div class="modal fade" id="contactModal" tabindex="-1" role="dialog" aria-labelledby="contactModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" id="contactModalLabel">Formulário para contato</h4> </div><div class="modal-body"> <form> <div class="form-group"> <label class="control-label">Seu nome:</label> <input type="text" class="form-control" name="name" placeholder="Escreva seu nome"> </div><div class="form-group"> <label class="control-label">Seu email:</label> <input type="email" class="form-control" name="email" placeholder="[email protected]"> </div><div class="form-group"> <label class="control-label">Assunto:</label> <input type="text" class="form-control" name="subject" placeholder="Escreva o assunto da mensagem"> </div><div class="form-group"> <label class="control-label">Mensagem:</label> <textarea class="form-control" id="mensagem" name="message" placeholder="Escreva sua mensagem"></textarea> </div></div><div class="modal-footer"> <button type="button" class="btn btn-default fechar" data-dismiss="modal">Fechar</button> <input type="submit" class="btn btn-primary fechar" value="Enviar mensagem"> </form> </div></div></div></div>

The redundancy of click and submit is due to the Stackoverflow preview sometimes not allow you to submit the form, but this is just a demo.

  • Aah thanks, it worked :) Taking advantage, would you have an answer to my second question? "... I would also like to ask why the alert takes (about 10 seconds) to appear on the screen. Is it because I’m using ajax on localhost?"

  • That doesn’t sound like a problem. The modal will only appear after the JS is loaded, it is necessary that Jquery and Bootstrap are loaded to appear. Except if set in CSS, so when CSS is loaded it will already be open, even without JS.

  • No, no... the problem is not in the modal... When clicking on "Send message" inside the modal, it takes about 10 seconds to exit the screen (with the Hide code you passed me) and the message alert sent only appears after that time too, IE, the JS takes to update the information that has been set (the alert appear and the modal hide)... Is this happening because I’m using ajax on localhost? It’s because I don’t like this delay when I move the site to hosting

  • The problem then is in response time. The "server" is taking a long time to respond. For example, if you are using PHP it is he who is slow to return the information to ajax. The success is done immediately after getting the server response.

  • Oh got it... Yes, I’m using PHP, would there be some way to speed up the response time? Because my code is simple, see: http://prntscr.com/duya2n

  • The mail() is a time-consuming topic at http://stackoverflow.com/questions/18325269/what-makes-phps-mail-function-so-slow. (in English). On the server this can be better than on the localhost, obviously.

  • Ah right, thank you for clearing up all my doubts and helping me :)

  • @Gabrielmoodlight, mark the answer. Do not abandon the question ;)

Show 3 more comments

-4

Parent. $('#meumodal'). modal('Hide');

-4

It worked for me this way:

jQuery('#modal').modal('hide');
  • Hello Augusto Lima, besides answering the question only with code, you could explain it and show how it would solve the Gabriel problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.