Home >CMS Tutorial >Empire CMS >How does Imperial ECMS implement AJAX without refreshing and adding to the shopping cart?

How does Imperial ECMS implement AJAX without refreshing and adding to the shopping cart?

angryTom
angryTomOriginal
2019-11-15 13:52:352147browse

How does Imperial ECMS implement AJAX without refreshing and adding to the shopping cart?

How does Empire ECMS implement AJAX without refreshing and adding to the shopping cart

Empire ECMS implements AJAX without refreshing and adding to shopping cart The main steps of the shopping cart method are as follows:

1. Modify the mall content template and add the following code to the head:

<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=&#39;<?=$public_r[newsurl]?>&#39;;
</script>

2. Modify the mall Content template, add the shopping cart part and modify it to the following code:

<a href="#ecms" onclick="AddProduct([!--classid--],[!--id--]);">加入购物车</a>

3. As for the jquery file, it is compatible with versions 1.3 and 1.4 and can be downloaded by yourself. The source code of the file addBuyCar.js is as follows:

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);
        }
    })
}

Fourth, add the handler e/template/ShopSys/addBuyCar.php, the source code is as follows:

<?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[&#39;classid&#39;];
 $id=(int)$_POST[&#39;id&#39;];
</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(&#39;msgid&#39;=>1, &#39;msg&#39;=>&#39;此商品不存在&#39;));
    }
    //验证产品是否存在 $num=$empire->gettotal("select count(*) as total from {$dbtbpre}ecms_".$class_r[$classid][tbname]." where classid=&#39;$classid&#39; and id=&#39;$id&#39; limit 1");
    if(!$num) {
        return json_encode(array(&#39;msgid&#39;=>1, &#39;msg&#39;=>&#39;此商品不存在&#39;));
    }
    $record="!";
    $field="|";
    $productid=$classid.",".$id;
    $buycar=getcvar(&#39;mybuycar&#39;);
    //重复 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[&#39;buycarnum&#39;]==1) {
            $buycar=&#39;&#39;;
        }
        $buycar.="|".$productid."|1!";
    }
    $re=SetBuycar($buycar);
    if($re) {
        return json_encode(array(&#39;msgid&#39;=>2, &#39;msg&#39;=>&#39;商品加入购物车成功&#39;));
    }
}
echo json_AddBuycar($classid, $id);
 db_close();
 $empire=null;
 ?>

At this point, the Empire ECMS AJAX function of adding to the shopping cart without refreshing has been completed.

Recommended tutorial: Empire CMS Tutorial

The above is the detailed content of How does Imperial ECMS implement AJAX without refreshing and adding to the shopping cart?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn