ecshop开发和使用过程中,你往往需要注意一个事项。那就是ecshop中ajax结合json的使用。最典型的例子就是用来更新数据,提交并用ajax返回。
首先在模板goods.dwt中。有一行代码{$lang.amount}:
{$lang.number}:这个是div用来显示ajax提交的数据,还有就是js函数changePrice()
function changePrice()
{
var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
Ajax.call('goods.php', 'act=price&id=' + goodsId + '&attr=' + attr + '&number=' + qty, changePriceResponse, 'GET', 'JSON');
}
把输入框中的数据传递给ajax.在goods.php中代码来处理他。他包含以下代码
include('includes/cls_json.php');
$json = new JSON;
$res = array('err_msg' => '', 'result' => '', 'qty' => 1);
产生结果 $res['result'] = price_format($shop_price * $number);用 die($json->encode($res));
返回给
function changePriceResponse(res)
{
if (res.err_msg.length > 0)
{
alert(res.err_msg);
}
else
{
document.forms['ECS_FORMBUY'].elements['number'].value = res.qty;
if (document.getElementById('ECS_GOODS_AMOUNT'))
document.getElementById('ECS_GOODS_AMOUNT').innerHTML = res.result;
}
}
首先在模板goods.dwt中。有一行代码{$lang.amount}:
{$lang.number}:这个是div用来显示ajax提交的数据,还有就是js函数changePrice()
function changePrice()
{
var attr = getSelectedAttributes(document.forms['ECS_FORMBUY']);
var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
Ajax.call('goods.php', 'act=price&id=' + goodsId + '&attr=' + attr + '&number=' + qty, changePriceResponse, 'GET', 'JSON');
}
把输入框中的数据传递给ajax.在goods.php中代码来处理他。他包含以下代码
include('includes/cls_json.php');
$json = new JSON;
$res = array('err_msg' => '', 'result' => '', 'qty' => 1);
产生结果 $res['result'] = price_format($shop_price * $number);用 die($json->encode($res));
返回给
function changePriceResponse(res)
{
if (res.err_msg.length > 0)
{
alert(res.err_msg);
}
else
{
document.forms['ECS_FORMBUY'].elements['number'].value = res.qty;
if (document.getElementById('ECS_GOODS_AMOUNT'))
document.getElementById('ECS_GOODS_AMOUNT').innerHTML = res.result;
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29753604/viewspace-1347325/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29753604/viewspace-1347325/