2016-11-05 27 views
0

我想創建一個購物車,我幾乎完成。我使用ajax進行動態搜索,並使用ajax添加到購物車,並使用jquery刷新特定的div時,點擊但我面臨一個問題。我的問題是數量問題。我使用的會話儲值 //這是我的會話更新代碼給移動時的輸入值,然後通過ajax保存它 - php/jquery -php

$con = mysqli_connect("localhost", "root" , "","atest"); 
session_start(); 
    require("functions.php"); 
    cart_session(); 
$id=$_POST['id']; 
     //echo $arr['cart']; 

     if(isset($_SESSION[$arr["cart"]][$id])){ 

      $_SESSION[$arr["cart"]][$id][$arr["quantity"]]++; 
      //redirect("http://localhost/my/work/sellingcart/index.php",true); 

     }else{ 

      $sql_s="SELECT * FROM product_1 
       WHERE p_id={$id}"; 
      //echo $sql_s; 
      $query_s=mysqli_query($con,$sql_s); 
      if(mysqli_num_rows($query_s)!=0){ 
       $row_s=mysqli_fetch_array($query_s); 

       $_SESSION[$arr['cart']][$row_s["p_id"]]=array(
      "{$arr["quantity"]}" => 1 
        ); 

       //redirect("http://localhost/my/work/sellingcart/index.php",true); 
      }else{ 

       $message="This product id it's invalid!"; 

      } 

     } 

//使用AJAX的更新車

<script>  
     $("#link").click(function(e) { 
    e.preventDefault(); 
    var id = $("#id").val(); 

    var dataString = 'id='+id; 
    $('#loading-image').show(); 
$(".form :input").attr("disabled", true); 
$('#remove_cart').hide(); 
$('#link').hide(); 
$(".container").css({"opacity":".3"}); 


$(".form :input").attr("disabled", true); 
$('#remove_cart').hide(); 
$('#link').hide(); 
    $.ajax({ 
    type:'POST', 
    data:dataString, 
    url:'add_cart.php', 
    success:function(data) { 
     $('#availability').html(data); 
    }, 
    complete: function(){ 
     $('#loading-image').hide(); 
     $(".form :input").attr("disabled", false); 
     $('#remove_cart').show(); 
     $('#link').show(); 
     $(".container").css({"opacity":"1"}); 


    } 
    }); 
//$("#chat").load(location.href + " #chat"); 
//$("#chat").load(location.href+" #chat>*",""); 
}); 


</script> 

enter image description here 這裏是圖像和紅色標記是我的問題。

我想更新我的購物車時,我給它的價值和移動它然後它更新我的會議通過ajax和PHP。

有什麼幫助嗎?我不想用戶可以單獨更新那裏每個購物車項目的數量。我希望它動態只是給數量的數量,然後移動它保存ajax。上述

$('input[name=quantityBox]').change(function() { ... }); 

在你的函數(),添加一個AJAX POST請求包含類似

var quantity = $('input[name=quantityBox]').val(); 
// var id = something; 
$.ajax({ 
    type:'POST', 
    data:"productId=" + id + "&updateQuantity=" + quantity, 
    url:'add_cart.php', 
    success:function(data) { 
     $('#availability').html(data); 
    }, 
    complete: function(){ 
     // anything you want to do on successful update of request 
    } 
}); 

在你的PHP功能上面,您:

+0

不,沒有問題。我完成它。但主要問題是更新數量。 –

+0

我只是需要一種方式,當我給數量的價值假設10然後我需要點擊更新按鈕,我只是想刪除所有carting產品的所有更新按鈕只是想給一個數值,當我點擊其他區域然後它使用ajax保存價值而不刷新 –

+0

我摔跤過一次熊。謝謝你,但ARR沒有定義,但它定義到另一個文件它的一個大文件。 請幫助我的事情。數量給和移動然後它與ajax保存。 現在你只需要給出數量,並且需要點擊每個數量的更新,但對任何人都無聊 –

回答

1

分配一個onchange事件您的數量輸入框檢查產品是否已經存在於用戶的購物車中。此時,更改數量。使用Ajax

<input id="qty<?php echo $row['p_id'] ?>" value="" onchange="save_quantity(<?php echo $row['p_id'] ?>)"> 

功能:

if(isset($_SESSION[$arr["cart"]][$id])){ 

     $quantity = $_POST['updateQuantity']; 
     $id = $_POST['productId']; 
     $_SESSION[$arr["cart"]][$id][$arr["quantity"]] = $quantity; 

} 
0

特別感謝Nvj

分配一個onchange事件您的數量輸入框

function save_quantity(x){ 
     var quantity=$("#qty"+x).val(); 

     $.ajax({ 
      type:'POST', 
      data:"updateQuantity=" + quantity+ "&id="+x, 
      url:'update_qty.php', 
      success:function(data) { 
       $('#availability').html(data); 
      }, 
      complete: function(){ 
       // anything you want to do on successful update of request 
      } 
     }); 

} 

php文件update_qty.php

session_start(); 
$qty = $_POST["updateQuantity"]; 
$p_id = $_POST["id"]; 

foreach($_SESSION['cart'] as $id => $value) { 
         if($id==$p_id) 
         echo $id; 
         $_SESSION['cart'][$id]['quantity']=$qty; 
        } 
相關問題