asp+ajax版的购物车asp
<!--#include file="inc/cart.asp" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>购物车-<%= sitename %></title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/cart.js"></script>
</head>
<body>
<!--#include file="top.asp" -->
<div class="gouwuche">
<div class="gouwuchebt">我的购物车</div>
<div class="gouwutopD2"><table width="100%" border="0" cellspacing="1" cellpadding="0" style="background:#f0f0f0; margin-top:15px;">
<tr>
<td width="5%" height="40" align="center" bgcolor="#f7f7f7"><strong>选项</strong></td>
<td width="9%" align="center" bgcolor="#f7f7f7"> </td>
<td width="53%" align="center" bgcolor="#f7f7f7"><strong>商品信息</strong></td>
<td width="8%" align="center" bgcolor="#f7f7f7"><strong>单价(元)</strong></td>
<td width="9%" align="center" bgcolor="#F7F7F7"><strong>数量</strong></td>
<td width="8%" align="center" bgcolor="#F7F7F7"><strong>金额</strong></td>
<td width="8%" align="center" bgcolor="#F7F7F7"><strong>操作</strong></td>
</tr>
<%
dim proId,act,qty,k,rs
act=Request.QueryString("act")
proId=Request.QueryString("proId")
qty=Request.QueryString("qty")
if act<>"" then
If uCart.CheckItem(proId)=False Then
set rs = Server.CreateObject("adodb.recordset")
rs.open "select productName,discount_price,pic from Product where id="&proId,conn,1,1
if not rs.eof then
call uCart.AddItem(proId,rs("productname"),rs("discount_price"),0,0,qty,rs("pic"))
end if
rs.close
set rs=nothing
else
call uCart.ModifItem(proId,qty,2)
end if
end if
if uCart.CheckEmtpy=False Then
myCart=uCart.ViewCart()
k=0
For i=LBound(myCart,2) To UBound(myCart,2)
If myCart(0,i)<>"" Then '以前的判断语句
%>
<tr rel="<%=myCart(0,i)%>">
<td height="35" align="center" bgcolor="#FFFFFF" class="td1"><input type="checkbox" class="btn_checkbox product_select check-product cur" checked="checked" /></td>
<td align="center" bgcolor="#FFFFFF" class="ww td2"><a href="prolast.asp?id=<%=myCart(0,i)%>"><img src="<%=myCart(6,i)%>" /></a></td>
<td bgcolor="#FFFFFF" class="ww td3"><a href="prolast.asp?id=<%=myCart(0,i)%>"><%=myCart(1,i)%></a></td>
<td align="center" bgcolor="#FFFFFF" class="td4"><span class="c_red" value="<%=myCart(2,i)%>">¥<%=FormatNumber(myCart(2,i),2,-1)%></span></td>
<td align="center" bgcolor="#FFFFFF" class="td5"><img src="images/jianhao.jpg" class="minusNum" style="cursor:pointer;" />
<input name="pro_num" type="text" class="input6" value="<%= myCart(5,i) %>" />
<img src="images/jiahao.jpg" class="addNum" style="cursor:pointer;" /></td>
<td align="center" bgcolor="#FFFFFF" class="td6"><p>¥<%=FormatNumber(myCart(5,i)*myCart(2,i),2,-1)%></p></td>
<td align="center" bgcolor="#FFFFFF" class="td7"><a href="javascript:;" class="do_delete">删除</a></td>
</tr>
<%
k=k+1
end if
Next
myprice=uCart.TPrice()
else
Response.Write("<td colspan='7' align='center' height='32'>暂无购买商品,请继续 <a href='product.asp' style='color:red'>购物</a></td>")
end if
%>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top:15px;">
<tr>
<td width="25%" height="45" bgcolor="#f2f2f2">
<input type="checkbox" class="btn_checkbox check-all cur" checked="checked" /> 全选
<a href="javascript:;" class="CartNum"><%= k %></a>
<a href="javascript:;">件</a></td>
<td width="62%" align="right" bgcolor="#f2f2f2" class="gouwu14">商品总价(不含运费和优惠扣减):<strong id="totalPrice">
<span class="total_pri_blod">¥<%
if uCart.CheckEmtpy then
Response.Write(0)
else
Response.Write(FormatNumber(myprice(0),2,-1))
end if
%></span>
</strong></td>
<td width="13%" bgcolor="#f2f2f2">
<div class="jiesuan7"><strong><a href="javascript:;" id="settlement">结 算</a></strong></div>
</td>
</tr>
</table>
</div>
<div class="clear"></div>
</div>
<!--#include file="bottom.asp" -->
</body>
</html>
cart.js
$(function(){
//getTotalPrice();
//手动修改商品数量
var regNum = /^\d+$/;
$(".changeNum").unbind("blur").bind("blur", function () {
var obj = $(this);
var product_num = obj.val();
if(regNum.test(product_num) && product_num > 0){
var buy = parseInt($(this).data("buy"));
product_num = parseInt(product_num);
changeProductNum(obj, product_num - buy,'input');
}else{
alert("商品数量格式不正确");
$(this).val($(this).data("buy"));
return;
}
});
//增加商品数量
$('.addNum').unbind("click").bind("click", function () {
var obj = $(this);
changeProductNum(obj, 1, 'add');
});
//减少商品数量
$('.minusNum').unbind("click").bind("click", function () {
var obj = $(this);
var $itemNum = $(this).closest("tr").find("input[name='pro_num']").val();
if(parseInt($itemNum) > 1){
changeProductNum(obj, -1, 'add');
}
});
//删除商品 展示弹框
$('.btn_del').click(function(){
$(this).next().show();
var url = $(this).attr('href');
});
//取消删除商品
$(".reset_delete").unbind("click").bind("click", function () {
$(this).parents(".pop_box").hide();
});
//执行删除商品
$(".do_delete").unbind("click").bind("click", function () {
//$(this).parents(".pop_box").hide();
var parent = $(this).parents('tr');
parent.remove();
getTotalPrice();
});
//批量删除购物车商品
$('#del-all').click(function(){
var product_id=[];
$('.product_select').each(function(index,element){
if($(this).prop("checked")){
product_id.push($(this).closest("tr").attr('rel'));
}
});
if(product_id.length == 0){
alert('请先选择商品');
return false;
}
if(confirm("确认删除商品")){
var del_confirm = function(product_id){
$("tr[rel='"+product_id+"']").remove();
getTotalPrice();
};
for(var i in product_id){
del_confirm(product_id[i]);
}
}
return false;
});
//勾选计算
$('.check-product').unbind("click").bind("click", function () {
$(this).toggleClass('cur');
var tmp=$('.check-product');
if(tmp.length==tmp.filter('.cur').length){
$('.check-all').addClass('cur');
$('.check-all').prop("checked",true);
}else{
$('.check-all').removeClass('cur');
$('.check-all').prop("checked",false);
}
getTotalPrice();
});
//全选/全不选计算
$('.check-all').click(function(){
$(this).toggleClass('cur');
if($(this).hasClass('cur')){
$('.check-product').addClass('cur');
$('.check-all').addClass('cur');
$('.check-product').prop("checked",true);
}else{
$('.check-product').removeClass('cur');
$('.check-all').removeClass('cur');
$('.check-product').prop("checked",false);
}
getTotalPrice();
});
//结算
$("#settlement").click(function(e){
e.stopPropagation();
e.preventDefault();
var product_ids=[],num_ids=[];
$('.check-product').each(function(index,element){
if($(this).prop('checked')){
var $tr2=$(this).closest("tr");
product_ids.push($tr2.attr("rel"));
num_ids.push($tr2.find("input[name='pro_num']").val());
}
});
if(product_ids.length < 1){
alert("请选择商品");
return ;
}
else{
$.post("changeProductNum.asp",{"act":"settlement","productIds":product_ids.toString(),"numIds":num_ids.toString(),rnd:new Date().getTime()},function(msg){window.location.href='shop.asp';});
}
});
});//the end of window.onload by quwei
function checkBtn(){
var tmp=$('.check-product');
if(tmp.length==tmp.filter('.cur').length){
$('.check-all').addClass('cur');
}else{
$('.check-all').removeClass('cur');
}
}
//商品数量变更
function changeProductNum(obj,inputVal,type){
var $tr = obj.closest("tr"),product_id=$tr.attr("rel");
var $itemNum = $tr.find("input[name='pro_num']");
if($tr.size() > 0){
$itemNum.val(parseInt($itemNum.val()) + inputVal);
var itemPrice = parseFloat($tr.find("td.td4 .c_red").attr("value"));
$tr.find("td.td6 p").text("¥ "+ (parseFloat($itemNum.val())*itemPrice).toFixed(2));
}
getTotalPrice();
}
//价格计算
function getTotalPrice(){
var total_price = total_number = 0;
$('.check-product').each(function(index,element){
if($(this).prop('checked')){
var $tr2=$(this).closest("tr");
total_price += parseFloat($tr2.find("td.td4 .c_red").attr("value")) * parseInt($tr2.find("input[name='pro_num']").val());
total_number += parseInt($tr2.find("input[name='pro_num']").val());
}
});
if($(".CartNum").size() > 0)$(".CartNum").html(total_number);
$('#totalPrice .total_pri_blod').html(total_price.toFixed(2));
}
changeProductNum.asp
<!--#include file="conn.asp" -->
<!--#include file="inc/cart.asp" -->
<%
dim act
act=Request.Form("act")
if act="settlement" then
call order_settlement()
end if
sub order_settlement()
productIds=Request.Form("productIds")
numIds=Request.Form("numIds")
uCart.RemoveAll()
Set rs = Server.CreateObject("ADODB.Recordset")
if InStr(productIds,",")>0 then
arrPro=split(productIds,",")
arrNum=split(numIds,",")
For i=0 To UBound(arrPro)
rs.open "select productName,discount_price,pic from Product where id="&arrPro(i),conn,1,1
if not rs.eof then
if uCart.CheckItem(arrPro(i))=False then
call uCart.AddItem(arrPro(i),rs("productname"),rs("discount_price"),0,0,arrNum(i),rs("pic"))
else
call uCart.ModifItem(arrPro(i),arrNum(i),2)
end if
end if
rs.close
Next
Else
rs.Open "select productName,discount_price,pic from Product where id="&productIds,conn,1,1
If Not rs.EOF Then
if uCart.CheckItem(productIds)=False then
call uCart.AddItem(productIds,rs("productname"),rs("discount_price"),0,0,numIds,rs("pic"))
else
call uCart.ModifItem(productIds,numIds,2)
end if
End If
rs.Close
End If
Set rs=Nothing
end sub
closeconn
%>
cart.asp
<%
Class UserCart
'建立购物车
Sub CreateCart()
Dim CFlag
dim mCart(6,0)
CFlag=CheckCart()
if CFlag=false then
For iCount =LBound(mCart,2) to UBound(mCart,2)
mCart(0,iCount)=""
next
Session("UserCart")=mCart
end if
End Sub
'检查购物车
Public Function CheckCart()
IF IsArray(Session("UserCart")) THEN
CheckCart = true
ELSE
CheckCart = false
END IF
End Function
'检查购物车是否为空
Function CheckEmtpy()
dim mCart
mCart=Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)<>"" then
CheckEmtpy=false
Exit Function
end if
next
CheckEmtpy=True
End Function
'添加商品
Function AddItem(aID,aName,aPrice1,aPrice2,aPrice3,aCount,aImage) '2008-12-25改进 by tommy 商品增加在最后
dim mCart
AddItem=false
if CheckItem(aID)=false then
'无此商品
mCart=Session("UserCart")
i =UBound(mCart,2)
i=i+1
Redim preserve mCart(6,i)
mCart(0,i)=aID 'productID
mCart(1,i)=aName '产品名称
mCart(2,i)=aPrice1 '产品原价
mCart(3,i)=aPrice2 '会员价
mCart(4,i)=aPrice3 'VIP价
mCart(5,i)=aCount '购买数量
mCart(6,i)=aImage '产品图片
session("UserCart")=mCart
AddItem=True
Else
'有此商品,则商品数量+1
ModifItem aID,aCount,0
AddItem=True
End if
End Function
'删除商品数量
Sub DelItem(mID) '改进2008-12-25by tommy 增加删除商品过程
dim mCart
'Response.Write(mID)
mCart = Session("UserCart")
mount=UBound(mCart,2)
If mID=mount Then
mount=mount-1
mCart(0,mount+1)=""
ReDim Preserve mCart(6,mount)
Else
mount=mount-1
For i=mID To mount
For j=0 To 6
mCart(j,i)=mCart(j,i+1)
Next
Next
mCart(0,mount+1)=""
ReDim Preserve mCart(6,mount)
End If
Session("UserCart")=mCart
End Sub
'修改商品数量
Sub ModifItem(mID,mCount,mFlag)
'mFlag-标志 0-添加 1-删除,2-修改 3-清空
'mID-商品ID
'mCount-修改商品的数量
dim mCart
mCart = Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)=mID then
select case mFlag
case 1
mCart(0,i)=Int(mCart(5,i))-Int(mCount)
case 0
mCart(5,i)= Int(mCart(5,i))+Int(mCount)
case 2
if mCount=0 then
mCart(5,i)=0
mCart(0,i)=""
else
mCart(5,i)=mCount
end if
case 3
mCart(5,i)=0
mCart(0,i)=""
end select
session("UserCart")=mCart
exit Sub
end if
next
End Sub
'查看推车
Function ViewCart()
dim mCart
mCart=session("UserCart")
ViewCart=mCart
End Function
'检查商品
Function CheckItem(cID)
dim mCart
mCart =Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)=cID then
CheckItem=True
exit Function
end if
next
CheckItem=false
End Function
'清空购物车
Sub RemoveAll()
dim mCart
mCart = Session("UserCart")
For i =LBound(mCart,2) to UBound(mCart,2)
mCart(0,i)=""
next
Session("UserCart")=mCart
End Sub
'商品总价值
Function TPrice()
dim mCart,i
dim OutPrice(3)
mCart=session("UserCart")
for i=LBound(mCart,2) to UBound(mCart,2)
if mCart(0,i)<>"" then
Outprice(0) = Int(Outprice(0)) + Int(mCart(2,i))*Int(mCart(5,i))
Outprice(1) = Int(Outprice(1)) + Int(mCart(3,i))*Int(mCart(5,i))
Outprice(2) = Int(Outprice(2)) + Int(mCart(4,i))*Int(mCart(5,i))
Outprice(3) = Int(OutPrice(3)) + Int(mCart(5,i))
end if
next
TPrice=OutPrice
End Function
End Class
'=================================================
'建立购物车对象,该对象用于直接在程序中调用
'来自QQ:1984756933
'=================================================
Dim uCart
Set uCart= new UserCart
uCart.CreateCart()
%>
上一篇:asp链接excel2016
下一篇:asp购物车源码
我要评论