将PHP变量传递给bootstrap模式

Raj*_*bir 7 php mysql jquery twitter-bootstrap

我有一个使用while循环创建的按钮.所以while循环为mysql表中的每一行创建一个表行.

我有一行代码用于按钮,它为表的每一行再次创建,每个按钮具有基于id该记录的不同值.

$order .= '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['ID'].'">Edit</a></td>';
Run Code Online (Sandbox Code Playgroud)

问题是我想使用它$row['ID']并在模态中查看它,所以我可以使用mysqli查询检索具有相同ID的记录.

这是模态的代码.

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Edit Data</h4>
                </div>
                <div class="modal-body">
                    i want to save the id in a variable here so i can use it in a php script for this modal
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

She*_*ary 17

如果,我告诉你了.

带模态触发按钮 $row['ID']

$order .= '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['ID'].'">Edit</a></td>';
Run Code Online (Sandbox Code Playgroud)

$row['ID']使用Ajax方法获取值的Bootstrap Modal事件

$(document).ready(function(){
    $('#myModal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).data('id');
        $.ajax({
            type : 'post',
            url : 'fetch_record.php', //Here you will fetch records 
            data :  'rowid='+ rowid, //Pass $id
            success : function(data){
            $('.fetched-data').html(data);//Show fetched data from database
            }
        });
     });
});
Run Code Online (Sandbox Code Playgroud)

模态HTML

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Edit Data</h4>
            </div>
            <div class="modal-body">
                <div class="fetched-data"></div> //Here Will show the Data
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

上次创建fetch_record.php,检索记录并以模态显示

<?php
//Include database connection
if($_POST['rowid']) {
    $id = $_POST['rowid']; //escape string
    // Run the Query
    // Fetch Records
    // Echo the data you want to show in modal
 }
?>
Run Code Online (Sandbox Code Playgroud)


Mar*_*lla 17

把它放在你的循环中

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#message<?php echo $row['id'];?>">Message</button>
Run Code Online (Sandbox Code Playgroud)
<div id="message<?php echo $row['id'];?>" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
        <?php echo $row['id'];?>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 惊人.无需使用JS或AJAX即可实现.你应该得到更多的赞成.非常感谢你! (2认同)
  • 一个或两个记录的绝佳选择...查询返回的任何选项都包含大量记录吗?此方法将为每条记录生成模态代码,这可能会过大。有什么建议吗? (2认同)