帝国ECMS实现AJAX无刷新加入购物车的方法主要步骤如下:
一、修改商城内容模板,头部加入如下代码:
<script type="text/javascript" src="[!--news.url--]skin/default/js/jquery.js"></script>
<script type="text/javascript" src="[!--news.url--]skin/default/js/addBuyCar.js"></script>
<script type="text/javascript">
var newsurl='<?=$public_r[newsurl]?>';
</script>
<script type="text/javascript" src="[!--news.url--]skin/default/js/addBuyCar.js"></script>
<script type="text/javascript">
var newsurl='<?=$public_r[newsurl]?>';
</script>
二、修改商城内容模板,加入购物车部分修改成如下代码:
<a href="#ecms" onclick="AddProduct([!--classid--],[!--id--]);">加入购物车</a>
三、至于jquery文件,兼容1.3和1.4版本,自行下载。文件addBuyCar.js源代码如下:
function AddProduct(classid,id) {
$.ajax({
type: "post",
url: newsurl+"e/template/ShopSys/addBuyCar.php",
data:"classid="+classid+"&id="+id,
dataType: "json",
success: function (json) {
alert(json.msg);
}
})
}
$.ajax({
type: "post",
url: newsurl+"e/template/ShopSys/addBuyCar.php",
data:"classid="+classid+"&id="+id,
dataType: "json",
success: function (json) {
alert(json.msg);
}
})
}
四、加入处理程序e/template/ShopSys/addBuyCar.php,其源代码如下:
<?php
require("../../class/connect.php");
require("../../class/db_sql.php");
require("../../data/dbcache/class.php");
require("../../class/ShopSysFun.php");
$link=db_connect();
$empire=new mysqlquery();</p> <p>//$_POST
$classid=(int)$_POST['classid'];
$id=(int)$_POST['id'];</p> <p>//add function
function json_AddBuycar($classid,$id){
global $class_r,$empire,$dbtbpre,$public_r;
$classid=(int)$classid;
$id=(int)$id;
if(empty($classid)||empty($id)||empty($class_r[$classid][tbname]))
{
return json_encode(array('msgid'=>1,'msg'=>'此商品不存在'));
}
//验证产品是否存在
$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where classid='$classid' and id='$id' limit 1");
if(!$num)
{
return json_encode(array('msgid'=>1,'msg'=>'此商品不存在'));
}
$record="!";
$field="|";
$productid=$classid.",".$id;
$buycar=getcvar('mybuycar');
//重复
if(strstr($buycar,"|".$productid."|"))
{
$pr=explode("|".$productid."|",$buycar);
$pr1=explode("!",$pr[1]);
$oldbuycar="|".$productid."|".$pr1[0]."!";
//数量
$pr1[0]=ReturnBuycarProductNum($pr1[0]);
if(empty($pr1[0]))
{
$pr1[0]=1;
}
$newnum=$pr1[0]+1;
$newbuycar="|".$productid."|".$newnum."!";
$buycar=str_replace($oldbuycar,$newbuycar,$buycar);
}
else
{
//只存放一个
if($public_r['buycarnum']==1)
{
$buycar='';
}
$buycar.="|".$productid."|1!";
}
$re=SetBuycar($buycar);
if($re)
{
return json_encode(array('msgid'=>2,'msg'=>'商品加入购物车成功'));
}
}
echo json_AddBuycar($classid,$id);
db_close();
$empire=null;
?>
require("../../class/connect.php");
require("../../class/db_sql.php");
require("../../data/dbcache/class.php");
require("../../class/ShopSysFun.php");
$link=db_connect();
$empire=new mysqlquery();</p> <p>//$_POST
$classid=(int)$_POST['classid'];
$id=(int)$_POST['id'];</p> <p>//add function
function json_AddBuycar($classid,$id){
global $class_r,$empire,$dbtbpre,$public_r;
$classid=(int)$classid;
$id=(int)$id;
if(empty($classid)||empty($id)||empty($class_r[$classid][tbname]))
{
return json_encode(array('msgid'=>1,'msg'=>'此商品不存在'));
}
//验证产品是否存在
$num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where classid='$classid' and id='$id' limit 1");
if(!$num)
{
return json_encode(array('msgid'=>1,'msg'=>'此商品不存在'));
}
$record="!";
$field="|";
$productid=$classid.",".$id;
$buycar=getcvar('mybuycar');
//重复
if(strstr($buycar,"|".$productid."|"))
{
$pr=explode("|".$productid."|",$buycar);
$pr1=explode("!",$pr[1]);
$oldbuycar="|".$productid."|".$pr1[0]."!";
//数量
$pr1[0]=ReturnBuycarProductNum($pr1[0]);
if(empty($pr1[0]))
{
$pr1[0]=1;
}
$newnum=$pr1[0]+1;
$newbuycar="|".$productid."|".$newnum."!";
$buycar=str_replace($oldbuycar,$newbuycar,$buycar);
}
else
{
//只存放一个
if($public_r['buycarnum']==1)
{
$buycar='';
}
$buycar.="|".$productid."|1!";
}
$re=SetBuycar($buycar);
if($re)
{
return json_encode(array('msgid'=>2,'msg'=>'商品加入购物车成功'));
}
}
echo json_AddBuycar($classid,$id);
db_close();
$empire=null;
?>
至此,问题搞定!