asp購物車源碼

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!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>無標題文檔</title>
<script type="text/javascript" src="js/jquery.js"></script>
<style type="text/css">
a{text-decoration:none;}
td,div{font-size:12px}
</style>
</head>

<body>
<%
dim action,bookid,aProducts,delid,i,conn,rs,sql,sNewProducts

set conn = Server.CreateObject("adodb.connection")
conn.connectionstring="provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("#dbFB90.Mdb")
conn.open

action=Request.QueryString("action")
if Request.QueryString("id")="" then
	bookid=Session("productlist")'//////////////////////////////////調入查詢物品的id
else
  if Session("productlist")="" then
	Session("productlist")=Request.QueryString("id")
	bookid=Request.QueryString("id")
  else
    if InStr(Session("productlist"),Request.QueryString("id"))<1 then
		bookid=Session("productlist")+","+Request.QueryString("id")'//////////////把id全部存儲到bookid中類似與數組
		Session("productlist")=bookid
    else
		bookid=Session("productlist")
    end if
  end if
end if

if Session("productlist")="" then'////////////////////////若id爲空,則說明用戶沒有購物
	bookid=0
end if

if action="del" then '刪除購物車中的某一件商品
	aProducts=Split(Session("ProductList"),",")
	delid=CStr(Trim(Request.QueryString("id")))
	For i=0 To UBound(aProducts)    '循環所有商品ID
		If trim(aProducts(i))<>delid then
			'不等於被刪除的ID時則保存進新的列表中
			sNewProducts = sNewProducts & "," & aProducts(i)
		end if
	Next
	Session("ProductList") = mid(sNewProducts,2)
	  if Session("ProductList")="" then
		bookid=0
	  else
		bookid=Session("ProductList")
	  end if
elseif action="delall" then
	Session("ProductList")=""
	response.Redirect("cart.asp")
end if
%>
<form name="myform" method="post" action="cart.asp">
  <input type="hidden" name="id" value="<%= bookid %>" />
  <table width="90%" border="1" cellspacing="5" cellpadding="3" style="border-collapse:collapse; margin-bottom:20px;">
  <tr>
    <td align="center">商品id</td>
    <td align="center">商品名稱</td>
    <td align="center">商品單價</td>
    <td align="center">訂購數量</td>
    <td align="center">商品金額</td>
    <td align="center">操作</td>
  </tr>
<%
'根據臨時存儲到Session裏的商品ID分別從商品數據庫循環調出商品顯示到購物車頁面,遇到重複ID不顯示
Dim Quatity,sum,aa '判斷input 名
sum=0
if bookid<>0 and bookid<>"" then
	set rs=Server.CreateObject("adodb.recordset")
	sql="select id,product_name,price,info from product where id in ("&bookid&") order by id" '這裏替換成實際的商品數據庫及字段
	rs.open sql,conn,1,1
	
	if not (rs.eof and rs.bof) then
		aa=1
		Do While Not rs.EOF
		
		Quatity = CInt(Request("sl"&aa))
		If Quatity <=0 Then Quatity = 1 '以下爲購物車每一件商品內容,包含ID、名稱、數量及取消
%>
  <tr>
    <td align="center"><%=rs("id")%></td>
    <td align="center"><a href="showproduct.asp?id=<%=rs("id")%>" title="<%=rs("info")%>" target="_blank"><%=rs("product_name")%></a></td>
    <td align="center"><span id="unitprice<%= aa %>"><%=rs("price")%></span></td>
    <td align="center">
    <input type="hidden" id="itemId<%= aa %>" value="<%= rs("id") %>" />
    <input type="button" value="-" οnclick="myfun(1,'<%= aa %>')" /> 
    <input name="sl<%=aa%>" id="sl<%= aa %>" type="text" value="<%= Quatity %>" size="3" readonly="readonly" /> 
    <input type="button" value="+" οnclick="myfun(2,'<%= aa %>')" /></td>
    <td align="center"><span id="total<%= aa %>"><%=(rs("price")*Quatity)%></span></td>
    <td align="center"><a href="?id=<%=rs("id")%>&action=del"><font color="#FF0000" size="4" style="font-weight:bold">×</font></a></td>
  </tr>
<%'循環讀取ASP購物車內的商品
			sum=FormatNumber(rs("price")*Quatity+sum,2)
			rs.movenext
			aa=aa+1
		Loop
	else
		response.Write("<tr><td align='center' colspan='6'>暫無選購的商品,請繼續<a href='products.asp'>購物</a></td></tr>")
	end if
	rs.close
	set rs=nothing
Else
	response.Write("<tr><td align='center' colspan='6'>暫無選購的商品,請繼續<a href='products.asp'>購物</a></td></tr>")
end if
conn.close
set conn=nothing
%>
</table>
</form>
<div style="text-align:right; width:90%">
<input type="button" value="清空購物車" οnclick="if(confirm('確定要清空購物車麼?')){window.location.href='?action=delall';}" />
總價:<span id="tcount"><%= sum %></span>元
<input type="button" value="繼續購物" οnclick="window.location.href='products.asp';" />
<input type="button" value="立即結算" οnclick="nextStep();" />
</div>
<script type="text/javascript">
function round(v,e){
	var t=1;
	for(;e>0;t*=10,e--);
	for(;e<0;t/=10,e++);
	return Math.round(v*t)/t;
}

function myfun(str,obj){
	var quantity,sum=0;
	if(str==1){
		if(document.getElementById("sl"+obj).value>1){
			document.getElementById("sl"+obj).value--;
		}
	}
	else{
		document.getElementById("sl"+obj).value++;
	}
	quantity=$("#sl"+obj).val();
	$("#total"+obj).text(round($("#unitprice"+obj).text()*quantity,2));
	
	for(var i=1;i<<%= aa %>;i++){
		sum+=round($("#total"+i).text(),2);
	}
	sum=round(sum,2);
	$("#tcount").text(sum);
	
	var strTemp;

	for(var j=1;j<<%= aa %>;j++){
		if(j==1){
			strTemp=$("#itemId"+j).val()+"|"+$("#sl"+j).val()+"|"+$("#total"+j).text();
		}
		else{
			strTemp+="#"+$("#itemId"+j).val()+"|"+$("#sl"+j).val()+"|"+$("#total"+j).text();
		}	
	}
	//alert(strTemp);
}

function nextStep(){
	var strTemp;

	for(var j=1;j<<%= aa %>;j++){
		if(j==1){
			strTemp=$("#itemId"+j).val()+"|"+$("#sl"+j).val()+"|"+$("#total"+j).text();
		}
		else{
			strTemp+="#"+$("#itemId"+j).val()+"|"+$("#sl"+j).val()+"|"+$("#total"+j).text();
		}	
	}
	$.post("saveData.asp",{str:strTemp,rnd:new Date().getTime()},function(msg){alert(msg);})	
}
</script>
</body>
</html>

cart.asp如上代碼

 

saveData.asp 如下代碼:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
dim conn,str,i,strArr,arrstr
set conn = Server.CreateObject("adodb.connection")
conn.connectionstring="provider=microsoft.jet.oledb.4.0;data source="&server.MapPath("#dbFB90.Mdb")
conn.open

str=request.Form("str")
strArr=split(str,"#")
for i=0 to ubound(strArr)
	arrstr=split(strArr(i),"|")
	conn.execute("insert into [orders](productId,Quantity,total) values("&arrstr(0)&","&arrstr(1)&","&arrstr(2)&")")
next
response.Write("success")
conn.close
set conn=nothing
%>


QQ:1984756933

Tel:18638696266       歡迎大家與我交流,對我的代碼提出寶貴意見!
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章