<?php
session_start();
include_once("../php/cart_config.php");
include_once("../php/cart_update.php");
include_once("../php/library.php");
?>
<!-- start of cart list -->
<div class="shopping-cart">
<h2>Your Shopping Cart</h2>
<?php
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<ol>';
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<li class="cart-itm">';
echo '<span id="del_item" class="remove-itm"><a href="'.$update_cart_url.'?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<h3>'.$cart_itm["name"].'</h3>';
echo '<div class="p-code">P code : '.$cart_itm["code"].'</div>';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div class="p-price">Price :'.$currency.$cart_itm["price"].'</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
}
echo '</ol>';
echo '<span class="check-out-txt"><strong>Total : '.$currency.$total.'</strong> <a href='.$checkout_url.'>Check-out!</a></span>';
}else{
echo 'Your Cart is empty dude!';
}
?>
</div>
<script type ="text/javascript">
$.ajax({
url: "http://4rtificial.co.za/ecommerce/php/cart_update.php",
cache: false
})
.done(function(php) {
$("#del_item").append(php);
});
</script>
</div>
IM試圖以某種方式從當一個項目 從我已插入使用底部的AJAX腳本IM的購物車中移除給人一種閃光停止頁面。現場版本是在這個地址LINK REMOVED的方式,我看到它在Chrome中工作,但Firefox的加載不同,這是爲什麼?使用Ajax防止閃存頁面上
這是更新的產品腳本:
<?php
include_once("../php/cart_config.php");
session_start();
//add item in shopping cart
if(isset($_POST["type"]) && $_POST["type"]=='add')
{
$product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
$return_url = base64_decode($_POST["return_url"]); //return url
//MySqli query - get details of item from db using product code
$results = $mysqli->query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
$obj = $results->fetch_object();
if ($results) { //we have the product info
//prepare array for the session variable
$new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>1, 'price'=>$obj->price));
if(isset($_SESSION["products"])) //if we have the session
{
$found = false; //set found item to false
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_itm["code"] == $product_code){ //the item exist in array
$qty = $cart_itm["qty"]+1; //increase the quantity
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$qty, 'price'=>$cart_itm["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrive old info and prepare array for session var
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
}
if($found == false) //we didn't find item in array
{
//add new user item in array
$_SESSION["products"] = array_merge($product, $new_product);
}else{
//found user item in array list, and increased the quantity
$_SESSION["products"] = $product;
}
}else{
//create a new session var if does not exist
$_SESSION["products"] = $new_product;
}
}
//redirect back to original page
header('Location:'.$return_url);
}
//remove item from shopping cart
if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
{
$product_code = $_GET["removep"]; //get the product code to remove
$return_url = base64_decode($_GET["return_url"]); //get return url
foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
{
if($cart_itm["code"]==$product_code){ //item exist in the list
//continue only if quantity is more than 1
//removing item that has 0 qty
if($cart_itm["qty"]>1)
{
$qty = $cart_itm["qty"]-1; //just decrese the quantity
//prepare array for the products session
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$qty, 'price'=>$cart_itm["price"]);
}
}else{
$product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
}
//set session with new array values
$_SESSION["products"] = $product;
}
//redirect back to original page
// header('Location:'.$return_url);
?>
<!-- this script works to update the removed item from cart on live server -->
<script type="text/javascript">
window.location = "<?php echo $return_url; ?>";
</script>
<?php
}
?>
僅供參考我只在添加到購物車時收到閃光燈,而沒有刪除。 OSX上的Chrome31。 –
你在哪裏附加remove-itm事件? – Bala
如果我在Firefox中輸入地址,並從購物車中刪除一個項目,然後我得到一個空白頁閃存我如何控制,我在哪裏我錯了 – Code