来自Jquery我已经从添加到购物车按钮获得了产品ID。每当我点击按钮时,我都会使用ajax代码发送cart.php文件。我假设AJAX类型是在cart.php中为此代码发送的GET请求?我检查过控制台,似乎没有任何执行:我没有包含PHP代码来获取$ row ['product_id'],因为这不是问题。
购物车功能 - 数量详情:我在basket.php中调用此函数。
function cart()
{
//die(print_r($_SESSION));
$total = 0;
$item_quantity = 0;
$item_name = 1;
$item_number = 1;
$amount = 1;
$quantity = 1;
$shipping_cart = 3.50;
foreach ( $_SESSION as $name => $value ) {
if ( $value > 0 ) {
if ( substr($name, 0, 8) == "product_" ) {
$length = strlen($name);
$id = substr($name, 8, $length);
if ( $id != 'id' ) { // This is ADDED.
$query = query("SELECT * FROM products WHERE product_id = " . $id . " ");
confirm($query);
while ( $row = fetch_array($query) ) {
$item_quantity += $value;
$product_image = $row['product_image'];
$product_price = $row['product_price'];
$sub = $product_price * $value;
$product = <<<DELIMETER
<tr>
<td>{$row['product_title']}<br>
<img width='100' src='assets/images/{$product_image}'>
</td>
<td>£{$product_price}</td>
<td>{$value}</td>
<td>£{$sub}</td>
<td><a class='btn btn-warning' href="cart.php?remove={$row['product_id']}"><span class='glyphicon glyphicon-minus'></span></a> <a class='btn btn-success' href="cart.php?add={$row['product_id']}"><span class='glyphicon glyphicon-plus'></span></a>
<a class='btn btn-danger' href="cart.php?delete={$row['product_id']}"><span class='glyphicon glyphicon-remove'></span></a></td>
</tr>
<input type="hidden" name="item_name_{$item_name}" value="{$row['product_title']}">
<input type="hidden" name="item_number_{$item_number}" value="{$row['product_id']}">
<input type="hidden" name="amount_{$amount}" value="{$row['product_price']}">
<input type="hidden" name="quantity_{$quantity}" value="{$value}">
DELIMETER;
echo $product;
$item_name++;
$item_number++;
$amount++;
$quantity++;
}
$fee = '5';
$_SESSION['item_total'] = $total += $sub;
$_SESSION['item_quantity'] = $item_quantity;
if($_SESSION['item_total'] > '100'){
$_SESSION['item_total'];
}
else{
$_SESSION['item_total'] += $fee;
}
}
}
}
}
}
?>
这是标题中的购物车链接:
<li style="text-decoration:none; font-size:16px; list-style-type:none; display:inline-block;"><a href="basket.php" id="cart" style='color:#fff;'><i class="fa fa-shopping-cart"></i> Basket <span id="qty" class="badge"></span></a></li>
包含jQuery的item.php文件:
<a href="cart.php?add=<?php echo $row['product_id']; ?>" class="btn btn-success btn-lg Prodadd" id="<?php echo $row['product_id']; ?>" style="border-radius:2px;color:#fff;">Add to basket</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.Prodadd').on("click", function(){
var qty = '<?php
echo isset($_SESSION['item_quantity']) ? $_SESSION['item_quantity'] : $_SESSION['item_quantity'] = "0";?>';
var product_id = $(this).attr("id");
$.ajax({
type: "GET",
url: "cart.php",
data: "add":product_id,
success: function(){
alert('added to basket');
$('#qty').html(qty);
}
});
return false;
});
});
</script>
cart.php文件:
<?php require_once("includes/db.php"); ?>
<?php
if (isset($_GET['add']) ) {
$query = query("SELECT * FROM products WHERE product_id=" . escape_string($_GET['add']) . " ");
confirm($query);
while ($row = fetch_array($query) ) {
if ($row['product_qty'] != $_SESSION[ 'product_' . $_GET['add'] ] ) {
$_SESSION['product_' . $_GET['add'] ] += 1;
redirect("basket.php");
} else {
set_message3("We only have " . $row['product_qty'] . " " . "items" . " " . "of" . " " . $row['product_title'] . " " . "left in stock");
redirect("basket.php");
}
}
}
?>