要将动态数据传递到引导模态 [英] To pass dynamic data to a bootstrap modal
问题描述
我正在尝试在模式上加载特定帖子的评论.为此,我需要将帖子ID传递给模态,然后获取相应的注释. 模态由以下触发:
I am trying to load the comments of a particular post on a modal. For this I need to pass the post id to the modal and then fetch the corresponding comments. The modal is triggered by the following:
<a class="xyz" data-toggle="modal" data-target="#compose-modal" data-id="<?php echo $list[$i]->getID(); ?>">View Comments</a>
模态在页面底部定义如下:
And the modal is defined at the bottom of the page as follows:
<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 is executed before the page is returned to the browser. Once you see the page in your browser, all the PHP has already been executed. What you probably want to do is use AJAX. Here is a general outline of how you would do it:
有一个PHP页面,该页面带有ID,并以JSON形式返回您想要的数据.
Have a PHP page that takes the ID and returns the data you want as a 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触发模式:
In your html, you should trigger the modal using an onclick attached to the "View Comments":
<a class="xyz" onclick = "launch_comment_modal(<?php echo $list[$i]->getID(); ?>)">View Comments</a>
然后,在底部显示您的其他javascript:
then,at the bottom with your other 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>
这篇关于要将动态数据传递到引导模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!