网络编程
位置:首页>> 网络编程>> Asp编程>> asp网上购物车实例代码(2)

asp网上购物车实例代码(2)

  发布时间:2007-10-03 13:43:00 

标签:购物车

  五、 AddtoCart.asp
  本页提供用户所选商品的详细信息,用户在此页可以修改订购数量。
  1、 添加一个记录集,命名为prodRec,设定记录源为SQL statement,输入:Select * FROM Products。在<Head>区中输入如下代码:

<Script Language=Javascript Runat=Server>
 function prodRec_onbeforeopen(){
 newSQL="Select * From Products Where ProductID="+Request("ProductID");
 prodRec.setSQLText(newSQL);
}
</Script>

此段代码在记录集打开之前执行,根据Products.asp传递来的ProductID参数,过滤记录集,获得用户选择的商品。
  2、 再添加一个记录集,命名为cartRec,该记录集的作用是根据用户的Session编号和选定的产品编号,确定所选商品是否已经在购物车中,如果已经在购物车中,则提取Cart表中的记录,提示用户该商品已选购,并让用户更改选购数量:

<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>

3、 <Body>中的代码:

<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>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com