从HTML数据ID获取PHP变量值 [英] GET PHP variable value from HTML data-id

查看:106
本文介绍了从HTML数据ID获取PHP变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候问一个问题简而言之,今天是当用户单击页面上的按钮时,出现一个弹出模式,该模式应获取PHP变量值.

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.

由于单击弹出模式btn时没有GET或POST请求,这意味着我无法使用上述两个选项获取变量值

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

尝试了以下内容:

`<a id="myid" data-id="<?php echo $userID ?>">link</a>
<script>
  $('#myid').data("id");
</script>`

现在,我在javascript变量中具有PHP $userID值.这引出了我的问题,什么是检索$userID变量并将其插入php变量的最有效方法.

Now I have the PHP $userID value in a javascript variable. 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.

其他信息

找到了这个问题关于SO,但它并不真正适用于我.

I found this question on SO but it is not really applicable to me.

我试图实现的示例图片

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">&times;</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>

推荐答案

首先,从<a></a>中删除id="myId". ID必须是唯一的.使用课程.

First of all, remove id="myId" from <a></a>. ID must be unique. Use class.

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>标记末尾的页脚中放置以下代码.

Put below code in footer before end of </body> tag.

<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也在脚本标记中使用.如果您打算更改此页面的名称,也可以在script标记中更改该页面的名称.两者有关.]

[NOTE: Remember, this page name modal_ajax.php is used in script tag also. 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">&times;</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>

已经回答了类似的问题.请查看通过Modal Bootstrap和获取php变量?

Already answered similar question. Please have a look Passing data via Modal Bootstrap and getting php variable?

这篇关于从HTML数据ID获取PHP变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆