我有一个会话数组$_SESSION['cart']['id']
,并希望显示$_SESSION['cart]['id']
中包含product_name和product_price值的每个项目。如何将其显示为html
<div class="col-md-3" id="cart">
<div class="col-md-2">
<p class="product-name"></p>
</div>
<div class="col-md-1">
<p class="product-price"></p>
</div>
<form method="POST" action="">
<button class="btn btn-primary" name="clear">Clear Cart</button>
</form>
</div>
jquery的
$('a').on('click', function(e){
var id = $(this).data('product-id');
var btn_content = $(this);
btn_content.html('Added');
$.ajax({
url: 'cart.php',
type: 'POST',
dataType: 'json',
data: {id: id},
success: function(products){
$('p.product-name').html(products['product-name']);
$('p.product-price').html(products['product-price']);
console.log(products);
}
});
e.preventDefault();
});
这是cart.php文件
<?php session_start() ?>
<?php require 'config.php' ?>
<?php
if (isset($_POST['id'])) {
$id = $_POST['id'];
$sql = "SELECT product_name, product_price FROM products WHERE id=".$id;
$result = $mysqli->query($sql) or die($mysqli->error);
$result = $result->fetch_assoc();
//$_SESSION['cart'] = array();
if (isset($_SESSION['cart'])) {
if (isset($_SESSION['cart'][$id])) {
unset($_SESSION['cart'][$id]);
}
}
$_SESSION['cart'][$id] = $result;
$cart = json_encode($_SESSION['cart']);
echo $cart;
}?>
我无法显示$_SESSION['cart']
中的所有对象。我无法弄清楚如何将数据显示为html