账号通
    

账号  

密码  

11268

查看

29

回复
主题:分享一个AJAX购物车功能,对官方自带的购物车不满意的进来看看 [收藏主题] 本贴被认定为精华 转到:  
jikeyshi 当前离线

263

主题

1

广播

2

粉丝
添加关注
级别:二年级

用户积分:2479 分
登录次数:250 次
注册时间:2007/11/24
最后登录:2015/4/16
jikeyshi 发表于:2012/1/11 11:07:06   | 只看该作者 查看该作者主题 楼主 
科汛在线考试系统(NET)

点击查看原图



演示地址:http://jiebao.gotoip4.com/Item/list.asp?id=926

楼下说方法。



点评 3

乱弹琴:5.0     说得很好:5.0     
veryp undefined undefined
完全可以做成订餐网站; 威望+1 发表于 2013/7/1 17:49:00 
veryp 乱弹琴:5 说得很好:5
楼主再做一个最新版本系统的啊,另外加上总金额就OK了; 威望+2 发表于 2013/7/1 17:49:00 
 
  支持(22) | 反对(22) 回到顶部顶端 回到底部底部
jikeyshi 当前离线

263

主题

1

广播

2

粉丝
添加关注
级别:二年级

用户积分:2479 分
登录次数:250 次
注册时间:2007/11/24
最后登录:2015/4/16
jikeyshi 发表于:2012/1/11 11:07:31   | 只看该作者 查看该作者主题 沙发 

第一步:在调用页面的模板增加如下代码:



                <div id="basketItemsWrap">

                </div>

<div style="height:30px;" id="shopbt">

<ul id='shopcart_bottom'><li><a href='/shop/payment.asp' ><img src='/shop/images/hesuan.gif'/></a></li><li><a href='/shop/shoppingcart.asp' ><img src='/shop/images/chakangouwuche.gif'/></a></li></ul>

</div>



注意:代码和JS是严格匹配的,修改代码标识后记得修改JS。



页面顶端增加JS如下:



<script>

$(document).ready(function(){



$.ajax({

  type:'POST',

  url:'/Plus/Ajaxs.asp',

  data:{pram:"show",Action:"addcart1"}, //初始化购物车内容

  success:function(theResponse){

   if($("#productID_").length<=0 && theResponse!=""){ //初始化购物车。

    $("#basketItemsWrap").html(theResponse);

   

   

    }

   }

  });





   $("#basketItemsWrap .pjian").live("click", function(event) {

  var PidSplitter=(this.id).split("_");

  var Pid=PidSplitter[1];

  $("#notificationsLoader").html('<img src="/images/loading.gif">');

  $.ajax({

   type:'POST',

   url:'/plus/Ajaxs.asp',

   data:{pid:Pid,Action:"addcart1",Pact:"jian"},

   success:function(theResponse){

     $("#basketItemsWrap table").remove();

     $("#basketItemsWrap").html(theResponse);

     $("#notificationsLoader").empty();  

    }

   })

  

  });

  

   $("#basketItemsWrap .pdel").live("click", function(event) {

  var PidSplitter=(this.id).split("_");

  var Pid=PidSplitter[1];

  $("#notificationsLoader").html('<img src="/images/loading.gif">');

  $.ajax({

   type:'POST',

   url:'/plus/Ajaxs.asp',

   data:{pid:Pid,Action:"addcart1",Pact:"del"},

   success:function(theResponse){

     $("#basketItemsWrap table").remove();

     $("#basketItemsWrap").html(theResponse);

     $("#notificationsLoader").empty();  

    }

   })

  

  });

  

   $("#basketItemsWrap .pjia").live("click", function(event) {

  var PidSplitter=(this.id).split("_");

  var Pid=PidSplitter[1];

  $("#notificationsLoader").html('<img src="/images/loading.gif">');

  $.ajax({

   type:'POST',

   url:'/plus/Ajaxs.asp',

   data:{pid:Pid,Action:"addcart1"},

   success:function(theResponse){

     $("#basketItemsWrap table").remove();

     $("#basketItemsWrap").html(theResponse);

     $("#notificationsLoader").empty();  

    }

   })

  

  });





$(".addcart").click(function(){

  var ProductIdValSplitter=(this.id).split("_");

  var ProductIdVal=ProductIdValSplitter[1];

  $("#notificationsLoader").html('<img src="/images/loading.gif">');

  $.ajax({

   type:'POST',

   url:'/Plus/Ajaxs.asp',

   data:{pid:ProductIdVal,Action:"addcart1"},

   success: function(theResponse){

     if (theResponse=='error'){

     $("#notificationsLoader").empty();            

          alert('对不起,您没有登录!');

     }else if (theResponse=='error1'){

     $("#notificationsLoader").empty();            

          alert('对不起,您所在的用户级别不能购买本商品!');

      

     }else if (theResponse=='error2'){

     $("#notificationsLoader").empty();            

          alert('对不起,网站不允许游客订餐,请注册会员!');

     }else{

      

     $("#basketItemsWrap table").remove();

     $("#basketItemsWrap").html(theResponse);

     $("#notificationsLoader").empty();  

         

     }

   

    }

   });

  });

});



</script>



第二步:修改AJAx后台文件/plus/ajaxs.asp,增加红色代码



Case "relativeDoc" relativeDoc

Case "getModelType" getModelType

Case "addCart" addShoppingCart

Case "GetPackagePro" GetPackagePro

Case "GetSupplyContact" GetSupplyContact

Case "HitsGuangGao" HitsGuangGao

Case "GetClubBoardOption" GetClubBoardOption

Case "getclubboard" GetClubboard

Case "GetClubPushModel" GetClubPushModel

Case "getclubboardcategory" getclubboardcategory

Case "getonlinelist" getonlinelist

Case "addcart1" addcart



修改函数

Function GetUserID()

    If KS.IsNul(KS.C("UserName")) Then

     If Not KS.IsNul(KS.C("CartID")) Then

      GetUserID=KS.C("CartID")

     Else

      Response.Cookies(KS.SiteSn)("CartID")=KS.MakeRandom(15)

      GetUserID=KS.C("CartID")

     End If

    Else

     GetUserID=KS.C("UserName")

    End If

End Function



添加两个函数



sub showcart()

   Set RS=Server.CreateObject("ADODB.RECORDSET")

   RS.Open "Select i.id,i.title,i.GroupPrice,i.Price_Member,i.Price,c.attr,c.amount from KS_Product i Inner Join KS_ShoppingCart c on i.id=c.proid where c.flag=0 and c.username='" & GetUserID & "'  and i.verific=1 order by i.id desc",conn,1,1

      if rs.recordcount=0 then ks.die "":response.End()

   str="<table  border='1' cellspacing='0' cellpadding='0' style='border-collapse:collapse;' align='center' ><tr><th><font color=brown>菜名</font></th><th width='65'>数量</th><th width='50'>价格</th></tr>"

   n=1

    Do While Not RS.Eof

  IF KS.C("UserName")<>"" Then

    If RS("GroupPrice")=0 Then

     RealPrice=RS("Price_Member")

    Else

     Dim RSP:Set RSP=Conn.Execute("Select Price From KS_ProPrice Where GroupID=(select groupid from ks_user where username='" & KS.C("UserName") & "') And ProID=" & RS("ID"))

     If RSP.Eof Then

    RealPrice=RS("Price_Member")

     Else

    RealPrice=RSP(0)

     End If

     RSP.Close:Set RSP=Nothing

    End If

  Else

    RealPrice=RS("Price")

  End If

  str=str&"<tr id='productID_"&rs("id")&"'><td><font color=brown>" & rs(1) & "</font></td><td width='65'><img src='/images/-.png' class='pjian' id=""productjian_"&rs("id")&""" style='cursor:hand;' /> "&rs("amount")&" <img src='/images/+.jpg' class='pjia' id='productjia_"&rs("id")&"' style='cursor:hand;'/>  <a href='#' onclick='return false;' class='pdel'  id='proddel_"&rs("id")&"'><font color=red>删</font></a></td><td width='50'>¥" & RealPrice*rs("amount") & "</td></tr>"

  n=n+1

    RS.MoveNext

    Loop

    rs.close:set rs=nothing

    str=str&"</table>"

    ks.die str

   

end sub



Sub addcart()

Dim Prodid:Prodid=KS.ChkClng(request("pid"))

Dim ProductList:ProductList=Session("ProductList")

Dim Num:Num=KS.ChkClng(request("num"))

Dim Attr:Attr=KS.DelSQL(UnEscape(Request("AttributeCart")))

if Num=0 then Num=1

Dim Pram:Pram=ks.s("pram")

Dim pact:pact=ks.s("pact")

if Pram="show" then

call showcart()

response.End()   

end if

if pact="jian" then

   Dim RSA:Set RSA=Server.CreateObject("ADODB.RECORDSET")

   rsA.open "select * from KS_ShoppingCart where flag=0 and attr='" & attr & "' and username='" & GetUserID & "' And proid=" & Prodid,conn,1,3



   if not (rsa.eof and rsa.bof)  then

       if rsa("amount")>1 then

      rsa("amount")=rsa("amount")-1

      else

      rsa.delete

      end if

      rsa.update

      

end if

rsa.close

set rsa=nothing

call showcart()

response.End()

end if

if pact="del" then

conn.execute(" delete from KS_ShoppingCart where flag=0 and attr='" & attr & "' and username='" & GetUserID & "' And proid=" & Prodid)

call showcart()

response.End()

end if

if Prodid=0 then KS.Die ""

SqlStr = "select * from KS_Config"

   Set RS = KS.InitialObject("ADODB.recordset")

   RS.Open SqlStr, Conn, 1, 3

   

    Dim Setting:Setting=Split(RS("Setting")&"^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^^%^","^%^")

  RS.Close:Set RS=Nothing

     Set KSUser=New UserCls

     LoginTF=KSUser.UserLoginChecked



if LoginTF=false and Setting(63)=0 then

ks.die "error2"

end if



   Set RS=Server.CreateObject("ADODB.RECORDSET")

   RS.Open "select top 1 arrGroupID From KS_Product Where id=" & Prodid,conn,1,1

   If RS.Eof And RS.Bof Then

      RS.Close : Set RS=Nothing

   ks.die "该商品不存在!"

   End If

   arrGroupID=RS(0)

   If Not KS.IsNul(arrGroupID) Then

     Set KSUser=New UserCls

     LoginTF=KSUser.UserLoginChecked

  if logintf=fasle then

   ks.die "error"

  RS.Close:Set RS=Nothing

  end if

     If KS.FoundInArr(arrGroupID,KSUser.GetUserInfo("GroupID"),",")=false Then

   RS.Close:Set RS=Nothing

   ks.die "error1"

  End If

   End If

   RS.Close  '购买验证完毕

   Set RSA=Server.CreateObject("ADODB.RECORDSET")

   rsA.open "select * from KS_ShoppingCart where flag=0 and attr='" & attr & "' and username='" & GetUserID & "' And proid=" & Prodid,conn,1,3



   if rsa.recordcount=0 then

      rsa.addnew

      rsa("flag")=0

      rsa("proid")=Prodid

      rsa("username")=GetUserID

      rsa("attr")=attr

      rsa("adddate")=now

      rsa("amount")=Num

      rsa.update

else

      rsa("amount")=rsa("amount")+1

      rsa.update

end if

rsa.close

set rsa=nothing



call showcart()

end sub



第三步:修改调用标签



增加订购按钮

<a href="{@linkurl}" id="featuredProduct_{@id}" class="addcart"><img src="/images/buy.jpg"  /></a>





不想修改的下载修改好的文件即可



[replyview]

 下载信息  [文件大小:13.95 KB 下载次数: 次]
点击下载文件:Ajaxs.rar
[/replyview]

 
  支持(20) | 反对(22) 回到顶部顶端 回到底部底部
otbbs 当前离线

1605

主题

12

广播

2

粉丝
添加关注
级别:高一年

用户积分:8980 分
登录次数:1225 次
注册时间:2006/8/7
最后登录:2023/11/29
otbbs 发表于:2012/1/11 11:55:02   | 只看该作者 查看该作者主题 藤椅 
 
  支持(15) | 反对(11) 回到顶部顶端 回到底部底部
1006298681 当前离线

1438

主题

0

广播

2

粉丝
添加关注
级别:九年级

用户积分:3011 分
登录次数:654 次
注册时间:2010/6/28
最后登录:2020/10/30
1006298681 发表于:2012/1/11 12:53:47   | 只看该作者 查看该作者主题 板凳 
 
  支持(8) | 反对(7) 回到顶部顶端 回到底部底部
mm692 当前离线

36

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:45 分
登录次数:3 次
注册时间:2011/12/20
最后登录:2012/8/1
mm692 发表于:2012/1/11 13:43:59   | 只看该作者 查看该作者主题 报纸 
科汛在线商城系统(NET)
不错不错 很有帮助 谢谢楼主
 
  支持(7) | 反对(6) 回到顶部顶端 回到底部底部
mm692 当前离线

36

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:45 分
登录次数:3 次
注册时间:2011/12/20
最后登录:2012/8/1
mm692 发表于:2012/1/11 13:44:51   | 只看该作者 查看该作者主题 地板 
 
  支持(5) | 反对(4) 回到顶部顶端 回到底部底部
soces 当前离线

68

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:81 分
登录次数:22 次
注册时间:2012/1/2
最后登录:2013/4/17
soces 发表于:2012/1/11 14:12:05   | 只看该作者 查看该作者主题 7楼 
 
  支持(2) | 反对(1) 回到顶部顶端 回到底部底部
zhijiejz 当前离线

10

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:8 分
登录次数:3 次
注册时间:2012/1/10
最后登录:2012/1/11
zhijiejz 发表于:2012/1/11 15:12:26   | 只看该作者 查看该作者主题 8楼 
科汛智能建站系统

哇塞,一个购物车这么复杂啊

 
  支持(0) | 反对(0) 回到顶部顶端 回到底部底部
lifest 当前离线

102

主题

0

广播

0

粉丝
添加关注
级别:一年级

用户积分:92 分
登录次数:19 次
注册时间:2009/9/1
最后登录:2012/6/3
lifest 发表于:2012/1/11 20:52:08   | 只看该作者 查看该作者主题 9楼 
科汛在线商城系统(NET)
看看先,不错的pp学习中
 
  支持(0) | 反对(0) 回到顶部顶端 回到底部底部
fengye002011 当前离线

69

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:81 分
登录次数:11 次
注册时间:2012/1/11
最后登录:2012/10/18
fengye002011 发表于:2012/1/12 11:11:35   | 只看该作者 查看该作者主题 10楼 
科汛在线网校系统
你的代码也太长了!有没有更简单的!!
 
  支持(0) | 反对(0) 回到顶部顶端 回到底部底部
<上一主题 | 下一主题 >
Powered By KesionCMS Version X1
厦门科汛软件有限公司 © 2006-2016 页面执行0.21875秒 powered by KesionCMS 9.0