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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="__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无刷新加入购物车(基本代码)的主要内容,如果未能解决你的问题,请参考以下文章

Ajax实现无刷新分页

Ajax无刷新删除GridView行(常用)

今晚直播:AJAX网页无刷新技术

AJAX中UPDATEPANEL配合TIMER控件实现局部无刷新

Ajax PHP JavaScript MySQL实现简易的无刷新在线聊天室

ajax无刷新删除复制 THINKPHP