我正在为学校开设网上商店。我正在尝试制作jquery/ajax
购物车系统。
现在这是我的jquery.load();
代码
//LOAD PHP FUNCTION TO ADD TO CART
$("#cart_page").load("cart.php?command=addToCart&productID="+productID+"&size="+size+"&amount="+amount+"&product_name="+product_name+"");
这是 cart.php 文件:
<?php
//ADD PRODUCTS TO CART
if($_GET['command'] == 'addToCart'){
/********* START SESSION *********/
session_start();
if($_GET['productID'] == "" || $_GET['amount'] < 1 || $_GET['size'] == "" || $_GET['product_name'] == "") return;
$max=count($_SESSION['cart']);
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
if($_GET['productID'] == $_SESSION['cart'][$i]['productid']){
if($_GET['size'] == $_SESSION['cart'][$i]['size'] ){
$product_exists = 1;
}
}
}
if($product_exists != 1){
$_SESSION['cart'][$max]['productid'] = $_GET['productID'];
$_SESSION['cart'][$max]['product_name'] = $_GET['product_name'];
$_SESSION['cart'][$max]['amount'] = $_GET['amount'];
$_SESSION['cart'][$max]['size'] = $_GET['size'];
}
}else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid'] = $_GET['productID'];
$_SESSION['cart'][0]['product_name'] = $_GET['product_name'];
$_SESSION['cart'][0]['amount'] = $_GET['amount'];
$_SESSION['cart'][0]['size'] = $_GET['size'];
}
if(is_array($_SESSION['cart'])){
$max=count($_SESSION['cart']);
$cart = <<<EOD
<div class="cart_page">
<div class="cart_product_line">
$max
<ul>
EOD;
for($i=0;$i<$max;$i++){
$arrayID = $i;
$cart_productID = $_SESSION['cart'][$i]['productid'];
$cart_productName = $_SESSION['cart'][$i]['product_name'];
$cart_amount = $_SESSION['cart'][$i]['amount'];
$cart_size = $_SESSION['cart'][$i]['size'];
$cart.= <<<EOD
<li>$arrayID</li>
<li>$cart_productID</li>
<li id="$cart_productID">$cart_productName</li>
<li class="amount">$cart_amount</li>
<li class="size">$cart_size</li>
EOD;
}
$cart.= <<<EOD
</ul>
</div>
</div>
EOD;
}
}
echo $cart;
?>
现在它做了我想要的一切。但是它给出的结果'echo $cart.php'
没有显示在我的 index.php 中
你们能帮帮我吗?
PS:
如果我去:
cart.php?command=addToCart&productID=28&amount=10&size=12&product_name=Test
它还给了这个
1
0
28
Test
10
12
在 HTML 中,因此脚本可以正常工作并返回 html!我在加载产品时也这样做,它就像一个魅力。