产品名 | ProductName |
产品编号 | ProductID |
单价 | Price |
分类编号 | CategoryID |
产品编号 | ProductID |
产品名 | ProductName |
价格 | Price |
客户Session标号 | SessionID |
订购数量 | Quantity |
产品编号 | ProductID |
客户Session编号 | SessionID |
订购数量 | Quantity |
产品浏览和选购 | Products.asp |
放入购物车 | AddtoCart.asp |
浏览购物车 | ViewCart.asp |
删除一个产品 | DeleteItem.asp |
更新购物车 | UpdateCart.asp |
确认订单 | SaveOrder.asp |
<Script Language=Javascript Runat=Server> function prodRec_onbeforeopen(){ newSQL="Select * From Products Where ProductID="+Request("ProductID"); prodRec.setSQLText(newSQL); } </Script> |
<Script Language=Javascript Runat=Server> Var Incart; //是否在购物车中 Var QtyinCart; //用户已经选购的数量 function cartRec_onbeforeopen(){ newSQL="Select * From Cart Where (SessionID="+'Session.SessionID'+") and (ProductID="+Request("ProductID")+")"; cartRec.setSQLText(newSQL); } function cartRec_ondatasetcomplete(){ if(cartRecrdset.getCount()==1) //过滤后记录集不为空,用户已选购了该商品 { Incart=true; QtyinCart=cartRec.fields.getValue("Quantity"); //取出用户已经选购的数量 } </Script> |
<Form Action="UpdateCart.asp" Method="Post"> 您选定的商品为: <%=prodRecorset.fields.getValue("ProductNAME")%> <%If Incart=true Then%> 您的购物车中已有 <%=QtyinCart%>件此商品,请更新您选购的数量: <Input Type="Text" Name="OrderQty" Value="<%=QtyinCart%>"> <%Else%> 请输入您的选购数量: <Input Type="Text" Name="OrderQty" Value="1"> <%End If%> <Input Name="prodID" Type="hidden" Value="<%=prodRec.fields.getValue("ProductID")%>"> //使用一个隐藏的编辑框,存放用户选定的产品编号,供UpdateCart.asp使用。 <Input Name="prodName" Type="hidden" Value="<%=prodRec.fields.getValue("ProductName")%>"> <Input Name="prodPrice" Type="hidden" Value="<%=prodRec.fields.getValue("Price")%>"> //使用三个隐含编辑框,向UpdataCart.asp传递产品编号、名称和价格。 <Input Type="Submit" Value="添加至购物车"> </Form> |
<Script Language=Javascript Runat=Server> function cartRec_onbeforeopen(){ if(Request("InCart")==0) { newSQL="Select * From Cart"; }//不在购物车中,取出所有数据 else { newSQL="Select * From Cart Where SessionID="+Session.SessionID; }//已经在购物车中,取出唯一的数据,准备修改数据 cartRec.setSQLText(newSQL); } function cartRec_ondatasetcomplete(){ if(Request("InCart")==0) //如果不在购物车中,添加新纪录 { fields=new Array("ProductID","ProductName"Quantity","SessionID"); values=new Array(Request("prodID"),Request("prodName"),Request("OrderQty"),Session.SessionID); cartRec.AddImmeidate(fields,values); } else//如果已经在购物车中,更新购买数量 { cartRec.fields.setValue("Quantity",Request("OrderQty")); } Response.Redirect("ViewCart.asp"); </Script> |
七、 ViewCart.asp
1、 添加记录集,命名为cartRec,设置数据源为Select * From Cart
2、 添加Grid控件,设定数据源为cartRec,显示三个字段:ProductName、Quantity、Price。添加两个Unbounded Column:
1) 显示总价格,Field/Expression为:=[Quantity]*[Price]。
2) 编辑/删除按钮,Field/Expression为:="<A Href=AddtoCart.asp?ProductID="+[ProductID]+"编辑/A>/<AHref=DeleteItem.asp?ProductID="+[ProductID]+">删除</A>"。这两个超链接分别将ProductID传递给AddtoCart.asp和DeleteItem.asp。
3、 添加指向SaveOrder.asp的链接
<A href="SaveOrder.asp">确认订单</A>
八、 DeleteItem.asp删除用户选定的一条购物车记录
1、 添加记录集cartRec,设置数据源为Select * From Cart
2、 添加代码:
<Script Language=Javascript Runat=Server> And (ProductID="+Request("ProductID")+")"; |
function cartRec_ondatasetcomplete() { while(!cartRec.EOF) { fields=new Array("ProductID","SessionID","Quantity"); values=new Array(cartRec.fields.getValue("ProductID"), carRec.fields.getValue("SessionID"), carRec.fields.getValue("Quantity")); orderRec.Addimediate(fields,values);//向Order表中添加新记录 cartRec.deleteRecord();//删除购物车中的一条记录 cartRec.Movenext();//继续处理下一条记录 } Response.Redirect("ViewCart.asp"); } |