shopaddtocart.asp

来自「ASP+SQL Server动态网站开发从基础到实践教程」· ASP 代码 · 共 166 行

ASP
166
字号
<%Response.buffer=TRUE%>
<!-- #include file="shop$db.asp" -->
<%
'*******************************************************
Dim prodid, quantity, arrCart, scartItem

Dim strAction
'*******************************
'判断是否是本页面提交
strAction = UCase(Left(Request.Form("action"),5))
if strAction<>"" then
   ReprocessForm
else
   ProcessNewadd
end if

' 要加入购物篮的新商品
Sub ProcessNewAdd()
	Dim rc
	ShopInit
	GetInputValues
	arrCart = Session("MyCart")
	scartItem = Session("cartItem")
	If scartItem = 0 and prodid="" Then
		Response.Redirect "shoperror.asp?msg=" & Server.URLEncode ("购物篮中目前没有商品!")
	End If
	If prodid <> "" Then
	    CartAddItem prodid, rc
	    if rc > 0 then
			sError="没有商品号为="  & prodid & "的商品"
	    end if 
	end if
	DisplayForm
end sub

Sub GetInputValues 
' 获得传递过来的数据
'  productid = 商品号
'  quantity  = 商品数量
'  db = database to change the session database
'
	Dim sOption
	Dim optionnum
	Dim maxFeatures
	
	'获取商品号和定购商品数量
	prodid = Request.Form("productid")
	quantity = Request.Form("quantity")
	if prodid ="" then
	    prodid=Request.querystring("productid")
	    quantity = Request.querystring("quantity")
	end if
	' 一个商品可以由四种不同的特征
	'cMaxFeatures为常量,值为4
	maxfeatures=cMaxFeatures
	Session("Maxfeatures")=maxfeatures
	for i =1 to MaxFeatures
	   sOption= "Feature" & i
	   ProductOptions(i)=request.form(soption)
	
	   If ProductOptions(i)="" then
	          ProductOptions(i)=request.querystring(soption)
	   end if
	next
end sub

Sub ReprocessForm
	arrCart = Session("MyCart")
	scartItem = Session("cartItem")
	Select Case strAction
	Case "继续选购"
	          if session("home") <> "" then
	              Response.Redirect Session("home")
	           else 
		          Response.Redirect "default.htm"
	           end if
	  
		
	Case "修改定购"
	
	Dim quantities(20)
		For i = 1 To scartItem
		Dim tquantity
		tquantity = Request.Form("Quantity" & Cstr(i))
	        if Not isnumeric(tquantity) then 
	          tquantity=1
	        end if
	        If tquantity < 0 then
	             tquantity = abs(tquantity)
	        end if 
	    	arrCart(cQuantity,i) = CInt(tquantity)
	        Quantities(i)=tquantity
		Next
		
		For i = 1 to scartItem
		Dim confirm
		confirm = Request.Form("selected" & CStr(i))
	'   '   如果数量为0,则删除该商品
		If confirm <> "yes" or  quantities(i)=0 Then  
	
			scartItem = scartItem - 1
			Dim x
			For x = 1 to UBound(arrCart,1)
					arrCart(x,i) = ""
			Next
			Dim n
			n = i
			while n < UBound(arrCart,2)
				For x = 1 to UBound(arrCart,1)
					arrCart(x,n) = arrCart(x,n + 1)
					arrCart(x,n + 1) = ""
				Next
				n = n + 1
			wend	
		end If
		Next
		Session("cartItem") = scartItem
		Session("MyCart") = arrCart
	
	
	Case "成交"
		Response.Redirect "shopcustomer.asp"
	
	End Select
	DisplayForm
End Sub

' 显示修改后的商品列表
Sub DisplayForm()
ShopPageHeader
arrCart = Session("MyCart")
scartItem = Session("cartItem")
FormatFormFields
ShopPageTrailer
end sub

' 格式化表单
Sub FormatFormFields
%>
<center>
<table border="0" width="600" cellpadding="4">
  <tr>
    <td width="100%" valign="top">
    <h3 align="center"><font face="Arial">定购的次数: <%=scartitem%></font></h3>
    </td>
  </tr>
  <tr>
    <td  bgcolor="#FAFAD2" valign="top">
    以下为你定购的商品<br><li>可以在数量文本框中修改数量然后单击按钮<修改定购>
     <br><li>要删除一个商品取消前面的检查框,然后单击按钮<重新订购>
 </table>
<P>
<FORM action="shopaddtocart.asp" method="POST">
<%
dim allowdelete
allowdelete="TRUE"
CartFormat allowdelete
%>
     
<INPUT name="action" type=submit value="继续选购"><INPUT name=action type=submit value="修改定购"> 
<INPUT name=action type=submit value="成交"><br>
<P><br> 
</P></FORM>
<%
End Sub
%>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?