一、好比咱们category.dwt 里有javascript
<a href='flow.php'><SPAN id='cart_count_all'>{insert name='cart_info'}</SPAN></A> php
<a onclick="javascript:addToCart_xaphp({$goods.goods_id});" style="cursor: pointer;">添加购物车</a>java
咱们如今须要实现点击按钮后页面不刷新去更新上面购物车数量 ajax
二、首先咱们须要调入系统sql
{insert_scripts files='transport.js,utils.js'} 数据库
这俩个js文件 是有关ajax实现效果的类问题 咱们大可不去管 只须要调入 下来咱们打开系统的common.js文件json
进行添加咱们本身的添加购物车函数数组
function addToCart_xaphp(goodsId, parentId) {session
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回调函数很重要.
三、以上系统内部访问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
四、 好比咱们添加购物车成功 咱们能够数组里定义 $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层
搞定。