我已经完成了购物车。我可以更新购物车,但是当我点击商品旁边的删除按钮时,它会被重定向到空白页面。 这是我的cart.php的顶部:
<?php
session_start();
include ("db_connection.php");//include database connection
if (isset($_SESSION['cart'])) {
if(isset($_GET['ebook_id']) && !isset($_POST['update'])){
$statement=$_SESSION['cart'];
$find=false;
$number=0;
for($i=0;$i<count($statement);$i++){
if($statement[$i]['ebook_id']==$_GET['ebook_id']){
$find=true;
$number=$i;
}
}
if($find==true){
$statement[$number]['quantity']=$statement[$number]['quantity']+1;
$_SESSION['cart']=$statement;
}else{
$ebook_title="";
$price=0;
$ebook_image="";
$query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']);
while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$ebook_title=$row['ebook_title'];
$price=$row['price'];
$ebook_image=$row['ebook_image'];
}
$newData=array('ebook_id'=>$_GET['ebook_id'],
'ebook_title'=>$ebook_title,
'price'=>$price,
'ebook_image'=>$ebook_image,
'quantity'=>1);
array_push($statement, $newData);
$_SESSION['cart']=$statement;
}
}
}else{
if(isset($_GET['ebook_id'])){
$ebook_title="";
$ebook_image="";
$price=0;
$query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']);
while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
extract ($row);
$ebook_title=$row['ebook_title'];
$ebook_image=$row['ebook_image'];
$price=$row['price'];
}
$statement[]= array('ebook_id' => $_GET['ebook_id'],'ebook_title' => $ebook_title,'ebook_image' => $ebook_image,'price' => $price,'quantity' => 1);
$_SESSION['cart']=$statement;
}
}
?>
这是我的购物车形式:
<form method="POST">
<table class="table">
<thead>
<tr>
<th colspan="2">E-book</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<tr>
<?php
if (isset($_POST['update'])){
$arrquantity=$_POST['quantity'];
//check validate quantity
$valid = 1;
for ($i=0; $i < count($arrquantity); $i++) {
if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) {
$valid=0;
break;
}
}
if ($valid == 1){
$cart = unserialize (serialize($_SESSION['cart']));
for ($i=0;$i<count($cart);$i++) {
$cart[$i]['quantity']=$arrquantity[$i];
}
}else{
$error= "Quantity is invalid";
}
$_SESSION['cart']=$cart;
}
//delete ebook in cart
$total=0;
if (isset($_SESSION['cart'])) {
$data=$_SESSION['cart'];
$total=0;
for ($i=0;$i<count($data);$i++) {
?>
<?php echo isset($error) ? $error :'';?>
<div class="ebook">
<td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td>
<td><?php echo $data[$i]['ebook_title'];?></td>
<td>£<?php echo $data[$i]['price'];?></td>
<td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td>
<td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td>
<td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a>
</div>
</tr>
<?php
$total=($data[$i]['price']*$data[$i]['quantity'])+$total;
}
}else{
echo "<p class='text-muted'>Your shopping cart is empty</p>";
}
?>
</tbody>
<tfoot>
<tr>
<th colspan="5">Total Price</th>
<th colspan="2" id="total">£<?php echo $total; ?></th>
</tr>
</tfoot>
</table>
</div>
<!-- /.table-responsive -->
<div class="box-footer">
<div class="pull-left">
<a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a>
</div>
<div class="pull-right">
<button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button>
<button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i>
</button>
</div>
</div>
</form>
最后,这是删除项目的jquery:
$(".delete").click(function(e){
e.preventDefault();
var id=$(this).attr('data-id');
$(this).parentsUntil('.ebook').remove();
$.post('./js/delete.php',{
Id:ebook_id
},function(a){
location.href="cart.php?";
});
以及从购物车会话中删除商品的delete.php页面:
<?php
session_start();
$statement=$_SESSION['cart'];
for($i=0;$i<count($statement);$i++){
if($statement[$i]['ebook_id']!=$_POST['ebook_id']){
$newData[]=array(
'ebook_id'=>$statement[$i]['ebook_id'],
'ebook_title'=>$statement[$i]['ebook_title'],
'price'=>$statement[$i]['price'],
'ebook_image'=>$statement[$i]['ebook_image'],
'quantity'=>$statement[$i]['quantity']
);
}
}
if(isset($newData)){
$_SESSION['cart']=$newData;
}else{
unset($_SESSION['cart']);
echo '0';
}
?>
我是电子商务网站的新手。我真的很感激任何帮助。 谢谢
答案 0 :(得分:0)
在删除页面上
<?php
if(isset($_POST['ebook_id'])){
$query=mysqli_query($con,"DELETE FROM ebooks WHERE ebook_id=".$_POST['ebook_id']);
if($query==true) {
return true;
}else {
return false;
}
}
你的JS代码应该是
$(".delete").click(function(e){
e.preventDefault();
var id=$(this).attr('data-id');
$.ajax({
type:"POST",
url: "delete.php",
data: {ebook_id: id},
}).done(function(data) {
$("#row_"+id+"").hide();
});
});
您的表格
<form method="POST">
<table class="table">
<thead>
<tr>
<th colspan="2">E-book</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<?php
if (isset($_POST['update'])){
$arrquantity=$_POST['quantity'];
//check validate quantity
$valid = 1;
for ($i=0; $i < count($arrquantity); $i++) {
if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) {
$valid=0;
break;
}
}
if ($valid == 1){
$cart = unserialize (serialize($_SESSION['cart']));
for ($i=0;$i<count($cart);$i++) {
$cart[$i]['quantity']=$arrquantity[$i];
}
}else{
$error= "Quantity is invalid";
}
$_SESSION['cart']=$cart;
}
//delete ebook in cart
$total=0;
if (isset($_SESSION['cart'])) {
$data=$_SESSION['cart'];
$total=0;
for ($i=0;$i<count($data);$i++) {
?>
<?php echo isset($error) ? $error :'';?>
//i don't know why you had open div here, so i replaced the div with tr and tr should have been inside the loop
<tr class="ebook" id="row_<?php echo $data[$i]['ebook_id'];?>">
<td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td>
<td><?php echo $data[$i]['ebook_title'];?></td>
<td>£<?php echo $data[$i]['price'];?></td>
<td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td>
<td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td>
<td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a>
</tr>
</tr>
<?php
$total=($data[$i]['price']*$data[$i]['quantity'])+$total;
}
}else{
echo "<p class='text-muted'>Your shopping cart is empty</p>";
}
?>
</tbody>
<tfoot>
<tr>
<th colspan="5">Total Price</th>
<th colspan="2" id="total">£<?php echo $total; ?></th>
</tr>
</tfoot>
</table>
</div>
<!-- /.table-responsive -->
<div class="box-footer">
<div class="pull-left">
<a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a>
</div>
<div class="pull-right">
<button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button>
<button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i>
</button>
</div>
</div>