2014-02-24 19 views
0
<?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.'">&times;</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 
} 
?> 
+0

僅供參考我只在添加到購物車時收到閃光燈,而沒有刪除。 OSX上的Chrome31。 –

+0

你在哪裏附加remove-itm事件? – Bala

+0

如果我在Firefox中輸入地址,並從購物車中刪除一個項目,然後我得到一個空白頁閃存我如何控制,我在哪裏我錯了 – Code

回答

0

因爲你已經設置

href="../php/cart_update.php?removep=001&return_url=aHR0cDovLzRydGlmaWNpYWwuY28uemEvZWNvbW1lcmNlL3Nob3Av" 

的OnClose布通。所以,而不是它,關閉按鈕事件上使用Ajax。

E.g:

<a href="#" onclick="remove_cart_product("001","aHR0cDovLzRydGlmaWNpYWwuY28uemEvZWNvbW1lcmNlL3Nob3Av")">×</a> 

在javascript中

function remove_cart_product(product_id,return_url) 
{ 
    // send post request. If you are using jQuery try this 
    var param = {"product_id" : product_id,"return_url" : return_url}; 
    $.get("YOUR PHP URL",param,function(data){ // you can also use post method -> $.post(... 
     // manipulate UI element 
    }) 
} 
0

我會推薦給攔截的 '添加' 按鈕的 '的onclick' 事件,並取消其默認行爲。例如。

$(".add_to_cart").on('click', function(e) { 
    e.preventDefault(); 
    e.stopPropagation(); 
    /* PERFORM YOUR ADD TO CART LOGIC HERE */ 
}); 

我想你會看到'flash'頁面,因爲表單實際上正在提交。這個應該做的是取消表單提交。改爲從click回調錶單提交表單(即將您的ajax呼叫置於此處)。

0

試試這個

$('.remove-itm a').click(function (e) 
{ 
    e.preventDefault(); 
    $.ajax({ 
    url: $(this).attr('href'), 
    cache: false 
    }).done(function(php) { 
     // response from server 
    }); 

}); 

一旦你得到了來自服務器的響應,你應該執行刪除或添加HTML,根據你從服務器獲得響應。

+0

但我想保留我的刪除項目的功能,我只是希望頁面在沒有閃存的情況下完成請求 – Code

+0

當您更改爲上述代碼時發生了什麼? – Bala

+0

是的,我看到上面的代碼在您的網站上工作。該項目正在被刪除。我可以看到,當我刷新頁面。那麼,爲什麼我提到,一旦你從服務器獲得成功響應,你必須從購物車div /刪除html,或者如果服務器發送購物車div的響應,請替換響應內容。 – Bala