我正在尝试将项目添加到购物篮但是为了这样做,我需要检索此刻未发生的行ID。
这是我到目前为止使用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>
这是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>
这是$ starters变量的php代码:
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;
}
似乎无法检索行ID,任何人都可以看到我哪里出错了?谢谢!