donglaogu3788 2017-03-14 23:05
浏览 16

检索要添加到购物车的行ID PHP

I'm trying to add items to the basket but to do so I need to retrieve the row id which isn't happening at the moment.

Here's what I have so far for my table with the href:

<div class="row col-md-8 col-md-offset-2 custyle">
    <div id="response"></div>
     <div id="cat0"><h2>Starters</h2></div>
<table class="table table-striped custab" id="menu-table">
<thead>
    <tr>
        <th>Item</th>
        <th>Description</th>
        <th>Price</th>
        <th class="text-center">Action</th>
    </tr>
</thead>
        <tdata>
<?php
// output data of each row

foreach ($starters as $row) { ?>
            <tr><td style="width:30%;"><b><?php echo $row['product_name'] ?></b></td>
            <td style="width:40%;"><?php echo $row['description'] ?></td>
           <td style="text-align: center">£<?php echo $row['price'] ?></td>
            <td class="text-center"> <a href="customerHome.php?id=<?php echo $row['productid'] ?>" id="basketButton" value="Add To Basket"><span class="glyphicon glyphicon-plus"></span> </a></td></tr>
               <input type="hidden" name="pid" value="<?php echo $row['productid'] ?>" />
                <?php
}
?>
</tdata>

</table>
</div>  

and this is the javascript:

   $(document).ready(function(){

   $(document).on('click', '#basketButton', function(e){

    e.preventDefault();


    var pid = $(this).closest("tr").find(".nr").text(); // get id of clicked row


   $.ajax({
      url: 'basket.php',
      type: 'POST',
      data: 'id='+pid,
      dataType: 'html'
   })
   .done(function(data){
      console.log(data); 

   })


  });
}); 

    </script>  

And this is the php code for the $starters variable:

function getProductsCat($category){
  include('database.php');
  $query = "SELECT * FROM products WHERE category = :category";
  $stmt = $db->prepare($query);
  $stmt->bindParam(':category', $category);
  $stmt->execute();
  $result = $stmt->fetchAll();
  return $result;
 }

Can't seem to retrieve the row id, can anyone see where I went wrong? Thanks!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用