码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-03-06 15:42:50      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:last   time   信用卡   product   价格   else   --   函数   cat   

<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="__HIMG__conduct-logo.png">
<ul class="left">
<volist name="cat" id="ca">
<input type="hidden" name="id" value="{$data[‘id‘]}">
<li class="cur"><a href="{:U(‘Index/products‘,‘cid=‘.$ca[‘id‘])}">{$ca[‘name‘]}</a></li>
</volist>
</ul>
<div class="right">
<div class="img">
<img class="bigimg" src="__ROOT__/Public/Uploads/{$data[‘image1‘]}">
<ul class="imglist">
<li><img src="__ROOT__/Public/Uploads/{$data[‘image1‘]}"></li>
<li><img src="__ROOT__/Public/Uploads/{$data[‘image2‘]}"></li>
<li><img src="__ROOT__/Public/Uploads/{$data[‘image3‘]}"></li>
<li><img src="__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="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="__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=‘productid=‘+productid+‘&num=‘+num;
var success=function(dat){
if(dat==8){
alert(‘请先登录!‘);window.location.href="{: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无刷新加入购物车(基本代码)

标签:last   time   信用卡   product   价格   else   --   函数   cat   

原文地址:http://www.cnblogs.com/xu--jun/p/6510092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!