0
我有一個產品旁邊有一個說「添加到購物車」的鏈接和一個添加到購物車選項的按鈕。鏈接不起作用,產品未添加。按鈕正常工作,產品已添加,但鏈接有問題。我是編程新手,如果我得到一些幫助,這將是美好的。添加到購物車功能不工作
這裏是javascript代碼。
$("body").on("click", ".addtocart", function() {
var product_id =$(this).data("product_id");
getCartCount(product_id);
});
function getCartCount(product_id){
$.ajax({
type:'POST',
url:'ajaxcallfunctions.php',
data:{
product_id:product_id,action:'addtocart'
},
success:function(data){
$("#shopped_count").html('('+data+')');
}
});
}
這是HTML鏈接
<html>
<a href="javascript:void(0)" class="addtocart" id="<?php echo $value['product_id']; ?>">Add to Cart</a>
</html>
這是PHP代碼
<?php
// ADD TO CART
if (isset($_POST['action']) && ($_POST['action'] == 'addtocart')) {
$product_id = (isset($_POST['product_id']) ? trim($_POST['product_id']) : '');
$user_id = $_SESSION['userid'];
if ($product_id != '') {
$data_set = array();
$sql = "SELECT product_id, product_name, product_price, description from admin_productdetails where product_id=$product_id ";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$data_set[] = $row;
}
}
$sql = "INSERT INTO user_productdetails (userid,product_id,product_name, product_price,description) "
. "VALUES ('" . $_SESSION['userid'] . "','" . $data_set[0]['product_id'] . "','" . $data_set[0]['product_name'] . "','" . $data_set[0]['product_price'] . "','" . $data_set[0]['description'] . "')";
$result = $connect->query($sql);
}
$sql = "SELECT COUNT(product_id) as count FROM user_productdetails where userid='$user_id' AND product_status='Pending'";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$product_count = $row['count'];
}
}
echo $product_count;
exit();
}
Add to Cart HTML鏈接。 –