编程语言
首页 > 编程语言> > php – 将动态数据传递给bootstrap模式

php – 将动态数据传递给bootstrap模式

作者:互联网

我试图在模态上加载特定帖子的评论.为此,我需要将post id传递给模态,然后获取相应的注释.
模态由以下内容触发:

<a class="xyz" data-toggle="modal" data-target="#compose-modal" data-id="<?php echo $list[$i]->getID(); ?>">View Comments</a>

模式在页面底部定义如下:

<div class="modal fade" id="compose-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        <!-- here i need to use php to fetch the comments using post id -->
        </div>
   </div>
</div>

解决方法:

在页面返回浏览器之前执行PHP.一旦在浏览器中看到该页面,就已经执行了所有PHP.您可能想要做的是使用AJAX.以下是如何执行此操作的概述:

有一个PHP页面,它获取ID并返回您想要的数据作为JSON.

api.php

   $theId = $_POST['theId'];

   //Get the information you want, and turn it into an array called $data

   header('Content-Type: application/json');
   echo json_encode($data);

在你的html中,你应该使用附加到“查看注释”的onclick来触发模态:

<a class="xyz" onclick = "launch_comment_modal(<?php echo $list[$i]->getID(); ?>)">View Comments</a>

然后,在底部与您的其他JavaScript:

   <script>
    $('#compose-modal').modal({ show: false});

    function launch_comment_modal(id){
       $.ajax({
          type: "POST",
          url: "api.php",
          data: {theId:id},
          success: function(data){

          //"data" contains a json with your info in it, representing the array you created in PHP. Use $(".modal-content").html() or something like that to put the content into the modal dynamically using jquery.

        $('#compose-modal').modal("show");// this triggers your modal to display
           },

    });

 }

    </script>

标签:php,jquery,modal-dialog
来源: https://codeday.me/bug/20191006/1858836.html