[英]Pass PHP variable to bootstrap modal
I have a button that is created using a while loop.我有一个使用 while 循环创建的按钮。 so the while loop creates a table row for each row in a mysql table.
所以 while 循环为 mysql 表中的每一行创建一个表行。
I have one line of code for the button which is created again for each row of the table and each button has a different value based on the id
for that record.我有一行代码用于为表格的每一行再次创建的按钮,并且每个按钮根据该记录的
id
具有不同的值。
$order .= '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['ID'].'">Edit</a></td>';
The problem is that I want to use that $row['ID']
and view it in a modal so I can retrieve the record with the same ID using mysqli query.问题是我想使用那个
$row['ID']
并以模式查看它,这样我就可以使用 mysqli 查询检索具有相同 ID 的记录。
Here is the code for the modal.这是模式的代码。
<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>
put this inside your loop把这个放在你的循环里
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#message<?php echo $row['id'];?>">Message</button>
<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">×</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>
If, I got you correct.如果,我猜对了。
The modal trigger button with $row['ID']
带有
$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>';
Bootstrap Modal event to fetch the $row['ID']
value with Ajax method Bootstrap Modal 事件使用 Ajax 方法获取
$row['ID']
值
$(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
}
});
});
});
Modal HTML模态 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>
Last Create fetch_record.php
, retrieve the record and show in modal最后创建
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
}
?>
adding the modal in a loop is auto duplicating the html content when it is not really necessary!在循环中添加模态是在不需要时自动复制 html 内容! it is not a good practice to add a modal to each item in a loop instead call the data only when it is requested!
向循环中的每个项目添加模式而不是仅在请求时调用数据不是一个好习惯!
here is an example add the following button to a loop in php这是一个示例,将以下按钮添加到 php 中的循环中
<button data-id="<?php echo $note['id']; ?>" onclick="$('#dataid').text($(this).data('id')); $('#showmodal').modal('show');">Click me </button>
on your modal you can know use that id to make a request to your db or any json在您的模态上,您可以知道使用该 ID 向您的数据库或任何 json 发出请求
example assuming you are using a modal called showmodal use this info to make a new request to your database or desire class or function!示例假设您正在使用名为 showmodal 的模态,使用此信息向您的数据库或所需类或函数发出新请求!
<?php $dataid = '<span id="dataid"/>'; echo $dataid; ?>
The echo $dataid is only to show you that the id of the clicked item in the loop really works. echo $dataid 只是为了告诉你循环中点击的项目的 id 确实有效。
as you can see here we are only adding 1 single html template in the loop and we are also only calling data when it is being requested.正如你在这里看到的,我们只在循环中添加了 1 个 html 模板,而且我们也只在数据被请求时调用它。 this will save memory and also will be faster.
这将节省内存,也将更快。
I hope this helps many people我希望这可以帮助很多人
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.