How to get Twitter Bootstrap modals to stay open on form submit?

∥☆過路亽.° 提交于 2019-11-28 20:57:24

When you form is submitted, the page is reloaded, even if the action of the form is the same page (empty action means the same page too).

I think you want to re-open the modal once the page is loaded again. Guessing that you are using a method="post" form, your code should be something like that :

<html>
    <head>
      <!-- stuff -->
        <script type="text/javascript">

<?php if(isset($_POST['submit_button'])) { ?> /* Your (php) way of checking that the form has been submitted */

            $(function() {                       // On DOM ready
                $('#myModal').modal('show');     // Show the modal
            });

<?php } ?>                                    /* /form has been submitted */

        </script>
    </head>
    <body>
      <!-- etc -->
    </body>
 </html>

In order not to close the modal window, that is, not to refresh the whole page, you need to submit the form values to your php script through ajax call.

For simplicity I will use jQuery here

$(function() {

    $('#your_form_id').on('submit', function(event) {

        event.preventDefault();

        $.ajax({
            url: "your_php_script.php",
            type: "POST",
            data: {"formFieldName" : formFieldValue}, // here build JSON object with your form data
            dataType: "json",
            contentType: "application/json"
        }).done(function(msg) {
            // this is returned value from your PHP script
            //your PHP script needs to send back JSON headers + JSON object, not new HTML document!
            // update your "message" element in the modal
            $("#message_element_id").text(msg);
        });
    });
};

You can also use window.stop() which will prevent the model from closing and the entire refresh all together, it's like clicking on the stop button in the browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!