我想创建一个购物车,而且我快完成了。我使用ajax进行动态搜索,使用ajax添加到购物车,并使用jquery在点击时刷新特定的div,但我遇到了问题。我的问题是数量问题。我使用session来储值 //这是我的 session 更新代码
$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>
我想在我提供值(value)并移动它时更新我的购物车,然后它通过 ajax 和 php 更新我的 session 。
有什么帮助吗?我不希望用户可以单独更新每个购物车项目的数量。我希望它是动态的,只需给出数量并移动,然后通过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;
}
关于javascript - 移动时给输入一个值,然后通过ajax保存它 - php/jquery -php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442783/