我今天早些时候问a question,简而言之,用户点击页面上的按钮时会出现一个弹出模式,它应该获得一个PHP变量值。
由于单击弹出模式btn时没有GET或POST请求,这意味着我无法使用上面两个选项获取变量值
尝试了以下内容:
`<a id="myid" data-id="<?php echo $userID ?>">link</a>
<script>
$('#myid').data("id");
</script>`
现在我在javascript变量中有PHP $userID
值。这引出了我的问题,检索$userID
变量并将其插入php变量的最有效方法是什么。
其他信息
我found this question关于SO,但它并不适用于我。
示例我想要实现的图像
LOOP生成的PHP列表
$teacherClass = new TeacherSearch();
$teachers = $teacherClass->showAllTeachers();
if (is_array($teachers)) {
foreach ($teachers as $teacher) {
$src = $teacher['userID'];
<div class="teacher-info">
<p class="teacherLabel">
NAME:
<?php
echo $teacher['name'];
?>
</p>
<p class="teacherLabel">
HEADLINE:
<?php
echo $teacher['headline'];
?>
<p class="teacherLabel">
LOCATION:
<?php
echo $teacher['location']
?>
</p>
<!--BUTTON GOES HERE-->
<a id="myid" data-id="<?php echo $teacher['userID'] ?>" data-toggle="modal" data-target="#myModal">Hire <?php echo $teacher['name'] ?></a>
}//foreach
示例IMG
模态
<!-- Trigger the modal with a button -->
<!-- Modal -->
<div id="myModal" 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 id="modalTitle" class="modal-title"></h4>
</div>
<div class="modal-body">
Need to perform PHP here with $userID variable
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
答案 0 :(得分:4)
首先,从id="myId"
移除<a></a>
。 ID必须是唯一的。使用课程。
<强> First_Page.php 强>
<?php
$teacherClass = new TeacherSearch();
$teachers = $teacherClass->showAllTeachers();
if (is_array($teachers)) {
foreach ($teachers as $teacher) {
$src = $teacher['userID'];
?>
<div class="teacher-info">
<p class="teacherLabel">
NAME:<?php echo $teacher['name']; ?>
</p>
<p class="teacherLabel">
HEADLINE:
<?php echo $teacher['headline'];?>
</p>
<p class="teacherLabel">
LOCATION: <?phpecho $teacher['location'];?>
</p>
<!--BUTTON GOES HERE-->
<a class="openModal" data-id="<?php echo $teacher['userID'] ?>" data-toggle="modal" href="#myModal">
Hire <?php echo $teacher['name']; ?>
</a>
</div>
<?php }
}?>
在</body>
代码结束前将代码放在页脚中。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<强> JS 强>
<script>
$('.openModal').click(function(){
var id = $(this).attr('data-id');
$.ajax({url:"modal_ajax.php?id="+id,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
创建新文件 modal_ajax.php 。
[注意:请记住,此页面名称modal_ajax.php也用于脚本标记。如果您打算更改此页面的名称,请在脚本标记中更改页面名称。两者都是相关的。]
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 id="modalTitle" class="modal-title"></h4>
</div>
<div class="modal-body">
<?php echo "ID: ".$_GET['id'];?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
已经回答了类似的问题。请看一下Passing data via Modal Bootstrap and getting php variable?