在 Web 開發中,通常需要將資料從連結傳遞到模態視窗。要在Bootstrap 中使用$_GET 變數實現此目的,請按照以下步驟操作:
模態呼叫按鈕:
<td>
莫代爾HTML:
將下列模態HTML 放置在呼叫按鈕所在頁面的while 循環之外(最好位於底部):
<div class="modal fade">
file.php:
<?php // Include database connection here $Id = $_GET["id"]; // Escape the string if you like // Run the query ?> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title"><center>Heading</center></h4> </div> <div class="modal-body"> // Show records fetched from database against $Id </div> <div class="modal-footer"> <button type="button" class="btn btn-default">Submit</button> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div>
<td>
<div class="modal fade">
// jQuery library comes first // Bootstrap library $(document).ready(function() { $('#editBox').on('show.bs.modal', function(e) { var id = $(e.relatedTarget).data('id'); // Fetch id from modal trigger button $.ajax({ type: 'post', url: 'file.php', // Here you will fetch records data: 'post_id=' + id, // Pass $id success: function(data) { $('.form-data').html(data); // Show fetched data from database } }); }); });
<?php // Include database connection here if ($_POST['id']) { $id = $_POST['id']; // Run the query // Fetch records // Echo the data you want to show in the modal } ?>
以上是如何將 $_GET 變數從連結傳遞到 Bootstrap 模式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!