2014-09-18 112 views
1

我想加載模態上特定帖子的評論。爲此,我需要將帖子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> 
+0

您是否嘗試過實現該目標? – jcvegan 2014-09-18 15:38:02

+0

[將數據傳遞給引導模式]可能的重複(http://stackoverflow.com/questions/10626885/passing-data-to-a-bootstrap-modal) – jcvegan 2014-09-18 15:39:22

+0

是的,我嘗試了很多選擇。使用以下內容:$('a [data-toggle = modal],button [data-toggle = modal]')。click(function(){ var data_id =''; if(typeof $ 。數據( '身份證')== '不確定'){ data_id = $(本)。數據( 'ID');} ! $( '#my_element_id')VAL(data_id); }) });我將模態中的輸入字段的值設置爲帖子ID。但我不能訪問在PHP形式的價值來獲取評論 – 2014-09-18 15:40:17

回答

4

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> 
+0

非常感謝!立即嘗試 – 2014-09-18 17:20:16

+0

模式不加載。是因爲默認引導? – 2014-09-18 18:21:32

+0

你是什麼意思的「不加載」?點擊時不顯示?你能看到控制檯中發生了什麼嗎? – 2014-09-18 18:35:48

0

只需將內容通過ajax與php頁面 和在模式中迴應內容