首页 > 代码库 > AJAX无刷新加入购物车(基本代码)

AJAX无刷新加入购物车(基本代码)

<form method="post" action="{:U(‘products‘)}">//用form表单传数据到脚本
<div class="content">
<div class="top">
<div class="line"></div>
<div class="title">
<span>PRODUCT CENTER</span>
<p>产品中心</p>
</div>
<div class="line"></div>
</div>
<div class="box">
<div class="con">
<img class="logo" src="http://www.mamicode.com/__HIMG__conduct-logo.png">
<ul class="left">
<volist name="cat" id="ca">
<input type="hidden" name="id" value="http://www.mamicode.com/{$data[‘id‘]}">
<li class="cur"><a href="http://www.mamicode.com/{:U(‘Index/products‘,‘cid=‘.$ca[‘id‘])}">{$ca[‘name‘]}</a></li>
</volist>
</ul>
<div class="right">
<div class="img">
<img class="bigimg" src="http://www.mamicode.com/__ROOT__/Public/Uploads/{$data[‘image1‘]}">
<ul class="imglist">
<li><img src="http://www.mamicode.com/__ROOT__/Public/Uploads/{$data[‘image1‘]}"></li>
<li><img src="http://www.mamicode.com/__ROOT__/Public/Uploads/{$data[‘image2‘]}"></li>
<li><img src="http://www.mamicode.com/__ROOT__/Public/Uploads/{$data[‘image3‘]}"></li>
<li><img src="http://www.mamicode.com/__ROOT__/Public/Uploads/{$data[‘image4‘]}"></li>
</ul>
<div class="clear"></div>
</div>
<div class="details">
<h1>{$data[‘name‘]}</h1>
<p class="p1">{$data[‘content‘]|htmlspecialchars_decode}</p>
<div class="d1">
<span class="s1">价格</span>
<span class="s2">{$data[‘price‘]}</span>
<span class="s3">累计评价</span>
<span class="s4">6666</span>
</div>
<h2>下午茶时间:13:00 - 17:00</h2>
<h2>全场五折</h2>
<span class="num">数量:</span>
<div class="num">
<span class="down">-</span>
<input type="text" id="num" name="num" value="http://www.mamicode.com/1">//id=#num
<span class="up">+</span>
</div>

<div class="pro-color" id="color">颜色分类:
<volist name="cdata" id=‘c‘>
<a style="cursor:pointer;" class="color" id="cid" onclick="color(this)" name="{$c[‘id‘]}">{$c[‘color‘]}</a>
<!-- -->
</volist>


<div class="clear"></div>
<button style="width:150px;height:46px;" type="submit">立即购买</button>
<a class="cart" href="javascript:void(0)" onClick="add_cart({$data[‘id‘]})">加入购物车</a>//用onclick点击调用下面的函数
<div class="clear"></div>
<span class="last">支付方式:</span>
<a class="last wx" href="javascript:;">微信</a>
<a class="last xy" href="javascript:;">信用卡支付</a>
<a class="last zfb" href="javascript:;">支付宝</a>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>

<div class="clear"></div>
</div>
<img src="http://www.mamicode.com/__HIMG__bg-bottom.png">
</div>
</div>
</form>
<script type="text/javascript">
function add_cart(productid){
var url="{:U(‘Vipcenter/add_cart‘)}";
var num=$("#num").val();
var data=http://www.mamicode.com/‘productid=‘+productid+‘&num=‘+num;
var success=function(dat){
if(dat==8){
alert(‘请先登录!‘);window.location.href="http://www.mamicode.com/{:U(‘Login/index‘)}";
}else if(dat==1){
alert(‘添加购物车成功!‘);
}else if(dat==0){
alert(‘添加购物车失败!‘);
}else{
alert(‘非法请求!‘);
}
}
$.post(url,data,success);
}
</script>

后台脚本

public function add_cart(){
layout(false);
if(AJAX){
$vipid=$_SESSION[‘vipname‘];
$data[‘vipid‘]=$vipid;
$data[‘pid‘]=I(‘post.productid‘);
$data[‘num‘]=I(‘post.num‘,‘‘,‘int‘);
$data[‘ontime‘]=time();
$result=M(‘shoppingcar‘)->where("vipid=‘$vipid‘ and pid=".$data[‘pid‘])->find();
if($result){
$data[‘num‘]=$result[‘num‘]+$data[‘num‘];
$result=M(‘shoppingcar‘)->where("vipid=‘$vipid‘ and pid=".$data[‘pid‘])->save($data);
}else{
$result=M(‘shoppingcar‘)->add($data);
}
if($result){
echo 1;
}else{
echo 0;
}
}else{
echo -1;
}
}

AJAX无刷新加入购物车(基本代码)