简体   繁体   English

如何从PHP调用Bootstrap Modal?

[英]How do I call Bootstrap Modal from PHP?

Quick question. 快速提问。 How do I call a Bootstrap Modal from PHP? 如何从PHP调用Bootstrap模式? I have a form and when some error happen I want to show an alert modal to the user. 我有一个表单,当发生某些错误时,我想向用户显示警报模式。 I've tried many solutions from other topics but neither has worked. 我尝试了其他主题的许多解决方案,但均无济于事。

Here's what I've tried last: 这是我上次尝试过的方法:

echo '<script type="text/javascript">';
echo '$(\'#alertDuplicate\').modal(\'show\')';
echo '</script>';

and here's the modal: 这是模态:

<!-- Modal Alert Duplicate -->
    <div class="modal fade" id="alertDuplicate">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Erro</h4>
                </div>
                <div class="modal-body">
                    <p>Essa função já existe!</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

Try putting that into a document ready function like this: 尝试将其放入文档就绪函数中,如下所示:

echo '<script type="text/javascript">';
echo '$(document).ready(function() {';
echo '$("#alertDuplicate").modal("show");';
echo '});';
echo '</script>';

You can use the example below or you can use ajax to call and return the response from your php script. 您可以使用下面的示例,也可以使用ajax调用并返回PHP脚本的响应。

echo <<<EOT
<script type="text/javascript">
$(document).ready(function() {
    $('#alertDuplicate').modal('show');
});
</script>
EOT;

AJAX EXAMPLE AJAX示例

With ajax you can use something like this: 使用ajax,您可以使用以下代码:

<script type="text/javascript">
$(document).ready(function () {
    $("input#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "YOUR_PHP_FILE.php",
            data: $('form.FORM_NAME').serialize(),
            success: function(msg){
                $("#alertDuplicate").modal('hide');
            },
            error: function(){
                $("#alertDuplicate").modal('show');
            }
        });
    });
});
</script>

(You have to change the words "FORM_NAME" and "YOUR_PHP_FILE" with your needs) (您必须根据需要更改单词“ FORM_NAME”和“ YOUR_PHP_FILE”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM