簡體   English   中英

在移動時給出一個輸入值,然后由ajax保存-php / jquery -php

[英]give a value to input when move then save it by ajax - php / jquery -php

我想創建一個購物車,我快要完成了。 我使用ajax進行動態搜索,使用ajax進行添加到購物車,並使用jquery刷新單擊時的特定div,但我遇到了問題。我的問題是數量問題。 我使用會話獲取存儲值//這是我的會話更新代碼

$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>

在此處輸入圖片說明 這是圖像,紅色標記是我的問題。

我想在提供價值並移動它時更新購物車,然后通過ajax和php更新我的會話。

有什么幫助嗎? 我不想用戶可以單獨更新每個購物車項目的數量。 我希望它動態,只需給出數量編號並移動,然后由Ajax保存。

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

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

在上面的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函數中,檢查產品是否已經存在於用戶的購物車中。 此時,更改數量。

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

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

 }

特別感謝Nvj

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

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

使用ajax的功能:

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;
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM