我正在使用面向对象编程php,我遇到了一些问题。这些项目已正确添加到购物车,但是当单击输入或按更新按钮更新项目以及小计和总值时,它不起作用。 这是我的购物篮页面:
<?php
$session = session::getSession('basket');
$objBasket = new basket();
$out = array();
if(!empty($session)){
$objCatalogue = new catalogue();
foreach($session as $key => $value) {
$out[$key] = $objCatalogue->getProduct($key);
}
}
require_once('_header.php');
?>
<div class="container">
<ol class="breadcrumb" style="margin-bottom: 5px;">
<li><a href="index.html">Home</a></li>
<li><a href="category.html">Fiction</a></li>
<li class="active">Shopping Cart</li>
</ol>
</div>
<div class="container">
<?php if (!empty($out)) { ?>
<div class="col-md-12" id="big_basket">
<div class="box">
<h2>Shopping cart</h2>
<div class="table-responsive">
<form action="" method="post" id="frm_basket">
<table class="table">
<thead>
<tr>
<th>E-book</th>
<th>Quantity</th>
<th>Price</th>
<th>Remove</th>
</tr>
</thead>
<?php foreach($out as $item){ ?>
<tbody>
<tr>
<td><?php echo helper::encodeHTML($item['ebook_title']); ?></td>
<td><input type="text" name="qty-<?php echo $item['id']; ?>" value="<?php echo $session[$item['id']]['qty']; ?>" class="fld_qty" /></td>
<td>£<?php echo number_format($objBasket->itemTotal($item['price'], $session[$item['id']]['qty']),2); ?></td>
<td><?php echo basket::removeButton($item['id']); ?></td>
</tr>
</tbody>
<?php } ?>
<?php if($objBasket->_sub_total != 0){ ?>
<tfoot>
<tr>
<th colspan="2">Subtotal</th>
<th>£<?php echo number_format($objBasket->_sub_total,2);?></th>
</tr>
<?php } ?>
<tr>
<th colspan="2">Total Price</th>
<th><strong>£<?php echo number_format($objBasket->_total,2);?></strong></th>
</tr>
</tfoot>
</table>
</form>
</div>
<!-- /.table-responsive -->
</div>
<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" class="update_basket"><i class="fa fa-refresh"></i>Update</button>
<button type="submit" class="btn btn-default"><a href="./?page=checkout">Checkout<i class="fa fa-chevron-right"></i></a></button>
</div>
</div>
</div>
<?php } else { ?>
<p>Your shopping cart is currently empty</p>
<?php } ?>
</div>
</div>
</div>
<?php require_once('_footer.php'); ?>
这是我的jquery应该允许立即提升篮子:
$(document).ready(function(){
function refreshBasket(){
$.ajax({
url: 'mod/basket_view.php',
dataType:'html',
success: function(data){
$('#big_basket').html(data);
initBinds();
},
error: function(data){
alert('Error occurred');
}
});
}
if($(".add_to_basket").length > 0){
$(".add_to_basket").click(function() {
var trigger = $(this);
var param = trigger.attr("rel");
var item = param.split("_");
$.ajax({
type: 'POST',
url: 'mod/basket.php',
dataType: 'json',
data: ({ id : item[0], job : item[1] }),
success: function(data){
var new_id = item[0] + '_' + data.job;
if(data.job != item[1]){
if(data.job == 0){
trigger.attr("rel", new_id);
trigger.text("Remove from basket");
trigger.addClass("red");
}else{
trigger.attr("rel", new_id);
trigger.text("Add to basket");
trigger.removeClass("red");
}
}
},
error: function(data){
alert("Error ocurred");
}
});
return false;
});
}
initBinds();
function initBinds(){
if ($('.update_basket').length > 0){
$('.update_basket').bind('click',updateBasket);
}
if ($('.fld_qty').length > 0){
$('.fld_qty').bind('keypress', function(e){
var code = e.keyCode ? e.keyCode : e.which;
if (code == 13){
updateBasket();
}
});
}
}
function updateBasket(){
$('#frm_basket : input').each(function(){
var sid = $(this).attr('id').split('-');
var val = $(this).val();
$.ajax({
type: 'POST',
url: 'mod/basket_qty.php',
data: ({ id: sid[1], qty: val}),
success: function(){
refreshBasket();
},
error: function(){
alert('Error occurred');
}
});
});
}
});