【问题标题】:How to fetch data from a variable passed to a modal?如何从传递给模态的变量中获取数据?
【发布时间】:2018-11-01 21:36:15
【问题描述】:

我有一个带有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 mysql ajax twitter-bootstrap


    【解决方案1】:

    您忘记了 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 调用应该填充 Modal 字段。

    【讨论】:

    • 你能根据我的 Jquery 代码告诉我它是如何完成的吗?我真的是 Ajax 的新手。
    猜你喜欢
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2019-08-24
    • 2011-02-14
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多