Search code examples
javascriptjqueryhtmlformstwitter-bootstrap

Bootstrap modal stay open after form submit and page refresh


I hava a bootstrap modal form that i use to send a 4 field message. After the submit button is press i want to show the user a "thank you" message. But instead of that my form is closing and the page is refreshing.

How can i do for the modal form to stay open in order to display below the submit button the message.

Thank you!

      <section class="modal-form">
  <!-- Modal Video first page -->
  <div class="modal fade" id="participa-modal" tabindex="-1" role="dialog" aria-labelledby="participa-modal">
    <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="myModalLabel">Participă la un demo!</h4>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <form id="form" method="post" class="contact" action="" enctype="multipart/form-data" data-parsley-validate>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="text" name="homepage_firstname" id="homepage_firstname" class="form-control-contact" placeholder="Nume" required>
                    </div>
                    <div class="form-group">
                      <input type="email" name="homepage_email" id="homepage_email" class="form-control-contact" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                      <input type="text" name="homepage_phone" id="homepage_phone" class="form-control-contact" placeholder="Telefon" data-parsley-type="number" minlength="10" maxlength="10" required>
                      <input type="hidden" name="inner_message" id="inner_message" value="Participare demo curs!">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <textarea class="form-control-contact" name="homepage_message" id="homepage_message" placeholder="Scrisoare de intentie"></textarea>
                    </div>
                  </div>
                </div>
                <div class="form-actions">
                    <input type="hidden" name="homepagesubmitted" value="TRUE" />
                    <button type="submit" class="btn orange sign-in-btn">Înscrie-te</button>
                </div>
                <?php echo $homepage_send ?>
              </form>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- End Modal Video first page -->
  </section>

UPDATE: Ok. So i've manage to make it work the following code:

$(function () {
    var frm = $('#participa-modal');
    frm.submit(function (ev) {
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
            alert('ok');
            location.reload();
            }
        });
    ev.preventDefault();
    });
});

The quetion is how can i replace alert('ok') to point for a thank you in the same pop-up under the SEND button.


Solution

  • $(".modal-body form").submit(function(e) {
        var url = "ActionScript.php"; // the script where you handle the form input.
        $.ajax({
               type: "POST",
               url: url,
               data: $(this).serialize(), // serializes the form's elements.
               success: function(data) {
                   $(this).html("Thank you!!!");
               }
             });
        e.preventDefault(); // avoid to execute the actual submit of the form.
    });
    

    check the above code for your requirement

    Terms Used

    $(".modal-body form") class and child form selector in order to target your submitting form

    .submit( to trigger event on submit of your form

    type: "POST" is for secure traversing of data, by method post

    .serialize() is to gather (assemble) and send the posting data

    success: is a callback in order to proceed while the submission is successful.