Al-mahbub Khan Al-mahbub Khan - 28 days ago 6
jQuery Question

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

I want to create a shopping cart and i'm almost finish. I use ajax for dynamic search and ajax for add to cart and use jquery for refresh a specific div when click but i face a problem.My problem is Quantity problem. I use session for store value
//this is my session update code

$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!";

}

}


//use ajax for update cart

<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
Here is image and Red mark is my problem.

i want to update my cart when i give value and move it then it update my session by ajax and php.

Is there any help? I don't want to user can update there quantity every cart item singly. i want it dynamic just give quantity number and move then it save by ajax.

Nvj Nvj
Answer

Assign an onchange event to your quantity input boxes:

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

In your function() above, add an AJAX POST request containing something like

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

In your PHP function above, you check whether the product already exists in user's cart. At that point, change the quantity.

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

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

 }
Comments