Ecshop ajax 局部刷新购物车效能

fieldset{padding:10px;}

Ecshop ajax 局部刷新购物车功能

1、比如我们category.dwt 里有

<a href=’flow.php’><SPAN id=’cart_count_all’>{insert name=’cart_info’}</SPAN></A>

<a onclick=”javascript:addToCart_xaphp({$goods.goods_id});” >

我们现在需要实现点击按钮后页面不刷新去更新上面购物车数量? ? 2、首先我们需要调入系统

{insert_scripts files=’transport.js,utils.js’}

?????? 这俩个js文件 是有关ajax实现效果的类问题??我们大可不去管 只需要调入 下来我们打开系统的common.js文件? ???进行添加我们自己的添加购物车函数

function addToCart_xaphp(goodsId, parentId){var goods = new Object();var spec_arr = new Array();var fittings_arr = new Array();var number = 1;var formBuy = document.forms[‘ECS_FORMBUY’];var quick = 0;

// 检查是否有商品规格

goods.quick = quick;goods.spec = spec_arr;goods.goods_id = goodsId;goods.number = number;//goods.guige = guige;goods.parent = (typeof(parentId) == “undefined”) ? 0 : parseInt(parentId);

Ajax.call(‘flow.php?step=add_to_cart’, ‘goods=’ + goods.toJSONString(), addToCartResponse_xaphp, ‘POST’, ‘JSON’);}

解释;a.点击按钮首先执行这个函数 获取我们的数据?ecshop二次开发整理Ecshop ajax应用讨论????????b. Ajax.call(‘flow.php?step=add_to_cart’, ‘goods=’ + goods.toJSONString(), addToCartResponse_xaphp, ‘POST’, ‘JSON’); 这是局部执行 也就是无刷新执行flow.php?step=add_to+cart 同时把数据通过POST提交,有关json具体原理想研究的朋友可以去网上找资料。? ?? ???c、addToCartResponse_xaphp回调函数很重要.??3、以上系统内部访问flow.php 那么我们当然要去flow.php??打开flow.php 我们找到 if($_REQUEST[‘step’] == ‘add_to_cart’ ){? ?? ?? ?添加购物车的程序 }? ?? ?解释:a、我们通过POST传递了数据 在.php文件当然需要接收。首先我们要调入json处理数据文件 include_once(‘includes/cls_json.php’);??这个文件很重要 即 js 和php 直接通过一种方式俩者进行数据交互??? ?? ?? ?? ???b、$result = array(‘error’ => 0, ‘message’ => ”, ‘content’ => ”, ‘goods_id’ => ”); 这个也是我们常用的 默认定义数组。? ?? ?? ?? ???c、$json??= new JSON;声明json类 ? ?? ?? ?? ???d、$goods = $json->decode($_POST[‘goods’]); 数据接收给$goods??4、 比如我们添加购物车成功 我们可以数组里定义 $result[‘error’]=0;??就是把我们用到的数据放入$result数组? ???形象点 个人理解就是 通过??die($json->encode($result));数据传递给我们刚才的回调函数? ? 下来我看回调函数的写法

function addToCartResponse_xaphp(result){if (result.error > 0){// 如果需要缺货登记,跳转if (result.error == 2){if (confirm(result.message)){location.href = ‘user.php?act=add_booking&id=’ + result.goods_id;}}// 没选规格,弹出属性选择框else if (result.error == 6){openSpeDiv(result.message, result.goods_id, result.parent);}else{alert(result.message);}}else{

getCartNumber();}

}function getCartNumber(){Ajax.call(‘transport.php?act=cart_number’, ”, getCartNumberResponse, ‘GET’, ‘JSON’);}

解释:回调函数里我们又调用 getCartNumber()函数 在这个函数里我们又一次利用ajax查询购物车数量transport.php代码

if($_REQUEST[‘act’] ==’cart_number’){include_once(‘includes/cls_json.php’);//$_POST[‘goods’] = json_str_iconv($_GET[‘goods_id’]);

$sql = ‘SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount’ .’ FROM ‘ . $GLOBALS[‘ecs’]->table(‘cart’) .” WHERE session_id = ‘” . SESS_ID . “‘ AND rec_type = ‘” . CART_GENERAL_GOODS . “‘”;$row = $GLOBALS[‘db’]->GetRow($sql);

if ($row){$number = intval($row[‘number’]);$amount = floatval($row[‘amount’]);}else{$number = 0;$amount = 0;}

$json = new JSON;$result[‘content’] = $number;die($json->encode($result));}

然后在传回回调函数

function getCartNumberResponse(result){//alert(result.content);var message = ‘(‘+result.content+’)’;document.getElementById(‘cart_count_all’).innerHTML = message;}

好 我们接收数据库查询到的数量 然后通过innerHTML写入到 cart_count_all层??搞定。

孤独是一种无奈的选择,孤独是因为没有找到合适的同行者,

Ecshop ajax 局部刷新购物车效能

相关文章:

你感兴趣的文章:

标签云: