我有一个使用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>';
问题是我想使用$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">×</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>
如果,我告诉你了.
带有$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>';
使用Ajax方法获取$row [‘ID’]值的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 } }); }); });
模态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">×</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>
最后创建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 } ?>