当我选择产品时,“数量”已通过但未在“查看购物车”页面上的NUMBERS(1,2或3)中显示。
让我感到困惑的是为什么会这样?任何帮助将不胜感激。
我试图在两个文件视图购物车和add_Ajax中使用ajax传递数量值,但是它不显示我选择的数量。
<form method="GET" action="add_Ajax.php">
<input type="hidden" value="<?php echo $row[0];?>" name="id">
<div class="col-md-6">
<div class="product-body">
<div class="product-label">
<div class="product-btns">
<div class="qty-input">
<span class="text-uppercase">QTY: </span> <select name="q" id="q" style="position:absolute; margin-left:10px; margin-top:-5px;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<script>
function additem(id,quantity)
{
q_index = document.getElementById("q").selectedIndex;
q = document.getElementById("q").options[q_index].value;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "add_ajax.php?id="+id+"&quantity="+q, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// alert('item added');
}
};
}
</script>
<h3 style="margin-top:20px;">Tale :-</h3>
<p style="font-size:16px;"><?php echo $row[8];?></p>
<button type="button" onClick="additem(<?php echo $row['id'];?>)" class="btn btn-info" data-toggle="modal" data-target="#myModal<?php echo $row['id'];?>">Add to cart</button>
<div id="myModal<?php echo $row[0];?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Details</h4>
</div>
<div class="modal-body">
<p>Item Added to cart
<?php echo $row[1];?>
<?php echo $row[2];?>
<?php echo $row[3];?>
<a href="cart.php"> <button type="button" class="btn btn-info" >View Cart</button></a>
<button type="button" class="btn btn-info" data-dismiss="modal">Continue Shopping</button>
</div>
</div>
</div>
</div>
</form>
<---add_Ajax.php---->
session_start();
$id = $_GET['id'];
$quantity = $_GET['q'];
echo "<script>alert('".$quantity."');</script>";
$con = mysqli_connect("localhost","root","") or die(mysqli_error($con));
mysqli_select_db($con,"booksjerry") or die(mysqli_error($con));
require_once("product.php");
$quantity = isset($_GET['q']) ? $_GET['q'] : 1;
$result = mysqli_query($con,"select * from products where id='".$id."'");
$row = mysqli_fetch_array($result);
$p = new product();
$p->img = $row['image'];
$p->id = $row['id'];
$p->name = $row['name'];
$p->price = $row['actualprice'];
$p->quantity = $quantity;
@$cart = unserialize(serialize($_SESSION['cart']));
print_r(@$cart);
$index=-1;
if($cart)
{
foreach(@$cart as $k=>$v)
{
if($v->id==$id)
{
$index = $k;
echo $index;
break;
}
}
}
echo 'index: '.$index;
if($index>=0)
{
$cart[$index]->quantity++;
$_SESSION['cart']= $cart;
@$c = (int) $_SESSION['count'];
$c = $c+1;
$_SESSION['count'] = $c;
//echo "found".$index;
}
else
{
echo 'not found';
$_SESSION['cart'][]= $p;
$_SESSION['count'] = 1;
}
//$_SESSION['cart'][]= $p;
//print_r($_SESSION['cart']);
if(isset($_GET['rem_id']))
{
require_once("product.php");
$id = $_GET['rem_id'];
@$cart = unserialize(serialize($_SESSION['cart']));
$index= -1;
foreach($cart as $k=>$v)
{
if($v->id==$id)
{
$index = $k;
echo $index;
break;
}
}
if($index>=0)
{
unset($cart[$index]);
$_SESSION['cart']= $cart;
}
}
?>
我想从产品页面传递我选择的数量以查看购物车和add_Ajax,但是它没有显示我选择的数量,显示为1。
答案 0 :(得分:0)
如果我答对了,那么您正在发送请求quantity
参数。因此,您可以在add_Ajax.php中使用$_GET['quantity']
而不是$_GET['q']
来引用它。
但是,如果您更改
xhttp.open("GET", "add_ajax.php?id="+id+"&quantity="+q, true);
到
xhttp.open("GET", "add_ajax.php?id="+id+"&q="+q, true);
然后效果更好。 :)