首页 > 解决方案 > 如何通过调用产品 id 来获取模式中的详细信息来制作模式引导弹出窗口?

问题描述

我试图通过单击详细信息按钮来制作弹出模式引导程序。在模式中,当我单击详细信息按钮时,我可以使用 pdo 调用产品详细信息,我可以获得产品编号(id)。不幸的是,productnum(id)总是调用最后一行数据productnum(id)......我如何正确调用产品productnum(id)以在模态引导javascript中插入?

 <?php
      // Read
         $per_page = 10;
      if (isset($_GET["page"]))
        $page = $_GET["page"];
      else
        $page = 1;
      $start_from = ($page-1) * $per_page;
      try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $conn->prepare("select * from tbl_products_a170804_pt2 LIMIT $start_from, $per_page");
        $stmt->execute();
        $result = $stmt->fetchAll();
      }
      catch(PDOException $e){
            echo "Error: " . $e->getMessage();
      }
      foreach($result as $readrow) {
      ?>   
      <tr>
        <td><?php echo $readrow['fld_product_num']; ?></td>
        <td><?php echo $readrow['fld_product_name']; ?></td>
        <td><?php echo $readrow['fld_product_price']; ?></td>
        <td><?php echo $readrow['fld_product_brand']; ?></td>
        <td><?php echo $readrow['fld_product_quantity']; ?></td>
        <td>
          <button class="btn btn-warning btn-xs openPopup" role="button" id="details" value="<?php echo $editrow['fld_product_num']; ?>" >Details</button>
           <?php if($pos==="Admin" || $pos==="Supervisor" ){ ?>
          <a href="products.php?edit=<?php echo $readrow['fld_product_num']; ?>" class="btn btn-success btn-xs" role="button"> Edit </a>
          <a href="products.php?delete=<?php echo $readrow['fld_product_num']; ?>" onclick="return confirm('Are you sure to delete?');" class="btn btn-danger btn-xs" role="button">Delete</a>
        </td>
      <?php } ?>
      </tr>
      <?php } ?>
 

这是我的模态引导代码

 <div id="myModal" class="modal fade" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">Product Details</h3>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">&times; Close</button>
            </div>
        </div>
    </div>
</div>

这是javascript ..当我点击它时,它总是给我最后一行ID

 <script>
$(document).ready(function(){
    $(".btn").click(function(){
        $('.modal-body').load('products_details.php?pid=<?php echo $readrow['fld_product_num']; ?>',function(){
    $('#myModal').modal({show:true});
});
    });
});
**问题:**如何正确调用确切的产品编号以放入模态 javascript

标签: javascripthtmljquerybootstrap-modal

解决方案


我已经解决了这个问题。只有我们数据-url

<button 
      data-href="products_details.php?pid=<?php echo $readrow['fld_product_num']; ?>"
       class="btn btn-warning btn-xs" role="button">Details</button>

这是我的jQuery

 <script>
$(document).ready(function(){
    $(".btn").click(function(){
       var dataURL = $(this).attr( "data-href" )
        $('.modal-body').load(dataURL,function(){
    $('#myModal').modal({show:true});
});
    });
});

推荐阅读