[英]GET PHP variable value from HTML data-id
I asked a question earlier today which in a nutshell asks when a user clicks a button on a page -- a pop up modal comes up, which should get a PHP variable value. 我今天早些时候问了一个问题 ,简而言之就是询问用户何时单击页面上的按钮-出现一个弹出模式,该模式应该获得PHP变量值。
Since there is no GET or POST request when popup modal btn is clicked which means I can not get the variable value using above two options 由于单击弹出模式btn时没有GET或POST请求,这意味着我无法使用上述两个选项获取变量值
tried the following: 尝试了以下方法:
`<a id="myid" data-id="<?php echo $userID ?>">link</a>
<script>
$('#myid').data("id");
</script>`
Now I have the PHP $userID
value in a javascript variable. 现在,我在javascript变量中具有PHP $userID
值。 Which leads me to my question what would be the most efficient way to retrieve the $userID
variable and insert it into a php variable. 这使我想到了一个问题:检索$userID
变量并将其插入php变量是最有效的方法。
Additional Info 附加信息
I found this question on SO but it is not really applicable to me. 我在SO上发现了这个问题 ,但它实际上不适用于我。
Example Image of what im trying to achieve 我试图实现的示例图像
LOOP GENERATED PHP LIST 生成的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
Example IMG 示例IMG
Modal 模态
<!-- 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>
First of all, remove id="myId"
from <a></a>
. 首先,从<a></a>
删除id="myId"
。 ID must be unique. ID必须是唯一的。 Use class. 使用类。
First_Page.php 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 }
}?>
Put below code in footer before end of </body>
tag. 将以下代码放在</body>
标记末尾的页脚中。
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
JS 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>
Create a new file modal_ajax.php . 创建一个新文件modal_ajax.php 。
[ NOTE: Remember, this page name modal_ajax.php is used in script tag also. [ 注意:请记住,此页面名称modal_ajax.php也在脚本标记中使用。 if you are planning to change the name of this page, change page name there in script tag too. 如果您打算更改此页面的名称,请在脚本标签中也更改该页面的名称。 Both are related.] 两者相关。]
<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>
Already answered similar question. 已经回答了类似的问题。 Please have a look Passing data via Modal Bootstrap and getting php variable? 请看看通过Modal Bootstrap传递数据并获取php变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.