Ecshop购物车Ajax数量添加减少

修改文件:

1. flow.dwt 模板, 

2. flow.php 

首先

第一步 :

修改购物车模板:

添加 数量减少/增加 按钮(这里用的span),input文本框按照下边参数设置好。

1<div class="zz fl"> 2 <span onclick="red_num({$goods.rec_id},{$goods.goods_id});">-</span> 3 <input type="text" name="goods_number[{$goods.rec_id}]" class="input2" value="{$goods.goods_number}" id="goods_number_{$goods.rec_id}" onchange="change_price({$goods.rec_id},{$goods.goods_id})" /> 4 <span onclick="add_num({$goods.rec_id},{$goods.goods_id})">+</span> 5</div>

第二步:

模板文件添加JS

1<script type="text/javascript"> 2 function add_num(rec_id,goods_id) 3 { 4 document.getElementById("goods_number_"+rec_id+"").value++; 5 var number = document.getElementById("goods_number_"+rec_id+"").value++; 6 7 Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON'); 8 } 9 function red_num(rec_id,goods_id) 10 { 11 if (document.getElementById("goods_number_"+rec_id+"").value>1) 12 { 13 document.getElementById("goods_number_"+rec_id+"").value--; 14 } 15 var number = document.getElementById("goods_number_"+rec_id+"").value; 16 Ajax.call('flow.php', 'step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON'); 17 } 18 function change_price(rec_id,goods_id) 19 { 20 var number = document.getElementById("goods_number_"+rec_id+"").value; 21 Ajax.call('flow.php','step=update_group_cart&rec_id=' + rec_id+'&number=' + number+'&goods_id=' + goods_id, changePriceRespone, 'GET', 'JSON'); 22 } 23 function changePriceRespone(result) 24 { 25 if(result.error == 1) 26 { 27 alert(result.content); 28 document.getElementById("goods_number_"+result.rec_id+"").value = result.number; 29 }else{ 30 31 document.getElementById('subtotal_'+result.rec_id).innerHTML = result.subtotal;//商品总价 32 document.getElementById('cart_amount_desc').innerHTML = result.cart_amount_desc;//购物车商品总价说明 33 document.getElementById('market_amount_desc').innerHTML = result.market_amount_desc;//购物车商品总市价说明 34 } 35 } 36 </script>

顺便模板文件数量、价格计算添加ID

1<span id="subtotal_{$goods.rec_id}">{$goods.subtotal}</span> 2 3<span id="cart_amount_desc">{$shopping_money}</span> 4 5<span id="market_amount_desc">{$market_price_desc}</span>

第三步:

修改php文件

搜索:$_REQUEST['step'] == 'consignee' 在上方添加

1elseif($_REQUEST['step']=='update_group_cart') 2{ 3 //2016-05-24 cart_update 4 include_once('includes/cls_json.php'); 5 $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => ''); 6 $json = new JSON; 7 $rec_id = $_GET['rec_id']; 8 $number = $_GET['number']; 9 $goods_id = $_GET['goods_id']; 10 $result['rec_id'] = $rec_id; 11 if ($GLOBALS['_CFG']['use_storage'] == 1) 12 { 13 $goods_number = $GLOBALS['db']->getOne("select goods_number from ".$GLOBALS['ecs']->table('goods')." where goods_id='$goods_id'"); 14 if($number>$goods_number) 15 { 16 $result['error'] = '1'; 17 $result['content'] ='对不起,您选择的数量超出库存您最多可购买'.$goods_number."件"; 18 $result['number']=$goods_number; 19 die($json->encode($result)); 20 } 21 } 22 $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number' WHERE rec_id = $rec_id"; 23 $GLOBALS['db']->query($sql); 24 /* 取得商品列表,计算合计 */ 25 $cart_goods = get_cart_goods(); 26 $subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id"); 27 $result['subtotal'] = price_format($subtotal, false); 28 $result['cart_amount_desc'] = sprintf($_LANG['shopping_money'],$cart_goods['total']['goods_price']); 29 $result['market_amount_desc'] = sprintf($_LANG['than_market_price'],$cart_goods['total']['market_price'],$cart_goods['total']['saving'],$cart_goods['total']['save_rate']); 30 31 die($json->encode($result)); 32}

到这里完成Ajax异步数量增减!!!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang