ECSHOP利用ajax更新购物车数量

前端之家收集整理的这篇文章主要介绍了ECSHOP利用ajax更新购物车数量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法一:

1.在flow.dwt中找到

<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center " onkeydown="showdiv(this)"/>
修改
<a href="javascript:;" onclick="chage_num({$goods.rec_id},{$goods.goods_id},'-');">-</a>
<input type="text" name="goods_number[{$goods.rec_id}]" id="goods_number_{$goods.rec_id}" value="{$goods.goods_number}" size="4" class="inputBg" style="text-align:center;" onblur="change_price({$goods.rec_id},{$goods.goods_id})"/>
<a href="javascript:;" onclick="chage_num({$goods.rec_id},'+');">+</a>
<td align="right" bgcolor="#ffffff">{$goods.subtotal}</td>
修改
<td align="right" bgcolor="#ffffff" id="subtotal_{$goods.rec_id}">{$goods.subtotal}</td>

{$shopping_money}
修改
<span id="cart_amount_desc">{$shopping_money}</span>

{$market_price_desc}
修改
<span id="market_amount_desc">{$market_price_desc}</span>
页面中插入js
<script>
function chage_num(rec_id,goods_id,type){
 if(type == '+'){
  document.getElementById("goods_number_"+rec_id+"").value++;
  var number = document.getElementById("goods_number_"+rec_id+"").value;
  Ajax.call('flow.PHP','step=update_group_cart&rec_id=' + rec_id +'&number=' + number+'&goods_id=' + goods_id,changePriceResponse,'GET','JSON');
 }
 if(type == '-'){
  if(document.getElementById("goods_number_"+rec_id+"").value < 1){
   var number = document.getElementById("goods_number_"+rec_id+"").value;
   Ajax.call('flow.PHP','JSON');
  }
  else{
   document.getElementById("goods_number_"+rec_id+"").value--;
   var number = document.getElementById("goods_number_"+rec_id+"").value;
   Ajax.call('flow.PHP','JSON');
  } 
 }
 if(number == 0){
  document.getElementById("goods_number_"+rec_id+"").value = 1;
 }
}

function changePriceResponse(result){
 if(result.error == 1){
  alert(result.content);
  document.getElementById("goods_number_"+result.rec_id+"").value =result.number;
 }
 else{
  document.getElementById('subtotal_'+result.rec_id).innerHTML = result.subtotal;//商品总价
  document.getElementById('cart_amount_desc').innerHTML = result.cart_amount_desc;//购物车商品总价说明 
 document.getElementById('market_amount_desc').innerHTML = result.market_amount_desc;//购物车商品总市价说明 
 }
}
</script>
2.在flow.PHP中插入
elseif($_REQUEST['step']=='update_group_cart'){
 include_once('includes/cls_json.PHP');
 $result = array('error' => 0,'message' => '','content' => '','goods_id' => '');
 $json = new JSON;
 $rec_id = $_GET['rec_id'];
 $number = $_GET['number'];
 $goods_id = $_GET['goods_id'];
 $result['rec_id'] = $rec_id;

 if ($GLOBALS['_CFG']['use_storage'] == 1){
  $goods_number = $GLOBALS['db']->getOne("select goods_number from ".$GLOBALS['ecs']->table('goods')." where goods_id='$goods_id'");
  if($number>$goods_number){
    $result['error'] = '1';
    $result['content'] ='对不起,您选择的数量超出库存您最多可购买'.$goods_number."件";
    $result['number']=$goods_number;
    die($json->encode($result));
  }
  elseif ($number<1){
   $result['error'] = '1';
   $result['content'] ='请最少购买1件';
   $result['number']='1';
   die($json->encode($result));
  }
 }
 $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number' WHERE rec_id = $rec_id";
 $GLOBALS['db']->query($sql);
 /* 取得商品列表,计算合计 */ 
 $cart_goods = get_cart_goods(); 
 $subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id"); 
 $result['subtotal'] = price_format($subtotal,false); 
 $result['cart_amount_desc'] = sprintf($_LANG['shopping_money'],$cart_goods['total']['goods_price']); 
 $result['market_amount_desc'] = sprintf($_LANG['than_market_price'],$cart_goods['total']['market_price'],$cart_goods['total']['saving'],$cart_goods['total']['save_rate']); 
 die($json->encode($result)); 
}


本文根据PHP分享网:http://www.xaphp.com/forum.php?mod=viewthread&tid=1142&extra=page%3D1中的方法修改而成,原视频中的方法有少许问题,均已修正。

原文链接:https://www.f2er.com/ajax/164613.html

猜你在找的Ajax相关文章