php - 如何从传递给模态的变量中获取数据?

标签 php jquery mysql ajax twitter-bootstrap

我有一个动态表 editdelete按钮。我能够传递变量 $parid模态。但是,它没有使用 $parid 将数据提取到模态中。 .我不知道我错过了什么。我只是想用mysql代码显示相应的数据WHERE parid='$parid' . $parid 的值传递给 <input type="text" name="parid" id="parid"> .这是我的示例代码:

      <table id="example" class="table table-striped" >
                    <thead>
                        <tr>
                            <th>Param ID</th>
                            <th>Decription</th>
                            <th>Code</th>
                            <th></th>
                         </tr>
                    </thead>
                    <tbody>
                      <?php
                        require 'lab/db/dbcon.php';
                        $query = $con->query("SELECT * FROM param");  
                            while($row=mysqli_fetch_array($query)){
                             ?>
                                <tr>
                                    <td><?php echo $row['parid'];?></td> 
                                    <td><?php echo $row['parnam']; ?></td> 
                                    <td><?php echo $row['parcod']; ?></td> 
                                    <td>
                                        <!--edit button here-->
                                        <a href="" class="btn btn-danger btn-xs btndel" data-toggle="modal" data-id="<?php echo $row['parid']; ?>" data-target="#delpar"><i class="fa fa-trash"></i>
                                        </a>
                                   </td> 
                            </tr>
                          <?php
                        }          
                      ?>
                    </tbody> 
                </table>

 <!-- Start Delete Par Modal -->
              <div class="modal fade" id="delpar" role="dialog">
                <div class="modal-dialog modal-sm">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      <h5 class="modal-title"><i class="fa fa-exclamation-circle"></i> Are you sure to delete this record?</h5>
                    </div>
                    <div class="modal-body">
                      <input type="text" class="form-control" name="parid" id="parid" disabled>
                      <table id="example" class="table table-bordered">
                    <thead>
                      <tr>
                        <th>Decription</th>
                        <th>Code</th>
                    </tr>
                    </thead>
                    <tbody>
                      <?php
                        require 'lab/db/dbcon.php';
                        $parid=isset($_POST['parid']);
                        $query = $con->query("SELECT * FROM param WHERE parid ='$parid' ");  
                            while($row=mysqli_fetch_array($query)){
                            ?>
                            <tr>
                                <td><?php echo $row['parnam']; ?></td> 
                                <td><?php echo $row['parcod']; ?></td> 
                            </tr>
                          <?php
                        }          
                      ?>
                    </tbody> 
                </table>
                    </div> 
                    <div class="modal-footer" style="padding:10px;">
                      <button type="button" class="btn btn-danger" name="btndel" id="btndel>"> <i class="fa fa-check"></i>
                      </button>
                      <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-remove"></i></button>
                    </div>
                  </div>
                </div>
              </div>
            <!-- End Delete Par Modal -->

             <script>
              $(document).on("click", ".btndel", function () {
                var parid = $(this).data('id');
                $(".modal-body #parid").val( parid );
                $('#delpar').modal('show');
              });
            </script>

最佳答案

您已经忘记了 PHP 和 jQuery 代码的范围和时间。 jQuery 部分:

    <script>
      $(document).on("click", ".btndel", function () {
        var parid = $(this).data('id');
        $(".modal-body #parid").val( parid );
        $('#delpar').modal('show');
      });
    </script>

在用户查看来自服务器的内容后在用户浏览器上运行。 “Delete Par Modal”中的 PHP 代码甚至在 jQuery 到达浏览器之前就在服务器上运行。

在您的 jQuery 中,您需要添加一个 AJAX 调用以从服务器(您的查询然后运行并返回数据的服务器)实时获取您需要的数据。然后“成功”AJAX 调用应该填充模态字段。

关于php - 如何从传递给模态的变量中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50479170/

相关文章:

php - 在搜索 PEAR HTML_Table 替换

php - 我在哪里可以找到 Simpletest 的扩展 HTML 报告程序?

javascript - backbone.js 嵌套模型的对象不是实例所独有的

javascript - onreset 后的 Jeditable 回调

mysql - 如何使用 GQL 选择看起来像这样的组( key ('Group' 、 '1779_1528' )

javascript - PHP如何一键打开2个链接 "_self "和 "_blank"不同的网址

php - 扩展 Wordpress 插件类

javascript - 如何通过在输入字段中输入十六进制颜色(使用或不使用 jQuery)来更改所需 div 的背景颜色?

mysql - 按降序排除重复项的行总和

mysql新用户访问被拒绝