zoukankan      html  css  js  c++  java
  • 用ASP编写购物车代码

    网上购物已成为生活的潮流,在网上购物之后,想要随时查看自己已买的东西,想要随时删除或改动某件商品数量,要怎么做呢?以下我就来写代码及释义。先来做用户登陆页面(login.asp):

    <html>

    <head>

    <title>购物车的实现</title>

    </head>

    <body>

    <formmethod="post"action="check.asp">

    <tablewidth="200"border="1">

    <tr><tdwidth="107">username:</td>

    <tdwidth="77"><inputtype="text"name="username"id="username"/></td></tr>

    <tr><td>password:</td>

    <td><inputtype="password"name="userpass"id="userpass"/></td></tr>

    <tr><tdcolspan="2"><inputtype="submit"value="登录"></td>

    </tr>

    </table>

    </form>

    </body>

    </html>

    然后来写接收用户所填写的信息以检查是否存在该用户的页面(check.asp),假设存在该用户,那么就跳到购物的首页(index.asp):

    <%

    uname=request.Form("username")

    upass=request.Form("userpass")

    sql="select*fromuserswhereuname='"&uname&"'andupass='"&upass&"'"

    setrs=server.CreateObject("adodb.recordset")

    rs.opensql,conn,3,1

    ifnotrs.eofthen

    用户登陆成功之后,将username放入session里,并给用户分配一个订单,就像去超市购物推着一个购物车,此时这个购物车是属于你的,用username和系统时间作为购物车的名字,然后跳转到购物首页。

    session("name")=uname

    session("orderid")=time&uname

    response.Redirect("index.asp")

    else

    %>

    <script>

    alert("username或password不对!");

    history.back();

    </script>

    <%

    endif

    rs.close

    setrs=nothing

    conn.close

    setconn=nothing

    %>

    那么,接下来就是购物的首页(index.asp)了

    <!--#includefile="inc/conn.asp"-->

    <html>

    <head>

    以下用css样式表将表格里的字体统一设置为14号字

    <styletype="text/css">‘

    <!--

    table{

    font-size:14px;

    }

    -->

    </style>

    </head>

    <body>

    <%

    假设用户没登陆就直接打开购物首页,那么就跳转到登陆页面

    ifsession("name")=""then

    response.Redirect("login.asp")

    endif

    %>

    做一个搜索的文本框,方便用户搜索商品。其原理是这种:用户填入要搜索的商品后,通过表单提交到本页面,从数据库中查找像用户填写的字符串的商品,再显示出来

    <formmethod="post"action="index.asp">

    <inputtype="text"size="15"name="search"id="search"/><inputtype="submit"value="搜索"/>

    </form>

    <%

    searchname=request.Form("search")‘得到用户填写的字符串

    sql="select*fromproductswherepnamelike'%"&searchname&"%'"‘写sql语句,查找出结果

    setrs=server.CreateObject("adodb.recordset")‘设置一个结果集对象

    rs.opensql,conn,3,1

    whilenotrs.eof‘循环输出商品的相关信息

    %>

    每一个商品以下都有一个购买button,须要做一个表单

    <formmethod="post"action="buy.asp">

    <tablestyle="float:left">

    <tr>

    <tdwidth="103"rowspan="3"><ahref="product.asp?id=<%=rs(0)%>"><imgsrc="images/<%=rs(2)%>"width="100"height="100"border="0"></a><inputtype="hidden"name="id"value="<%=rs(0)%>"><inputtype="hidden"name="price"value="<%=rs(3)%>"/></td>

    <tdwidth="107">名称:

    <%=rs(1)%></td>

    </tr>

    <tr>

    <td>价格:<%=rs(3)%></td>

    </tr>

    <tr>

    <tdalign="center"><inputtype="submit"name="buy"value="购买"/></td>

    </tr>

    </table>

    </form>

    <%

    rs.movenext‘指向下一个记录

    wend

    rs.close

    setrs=nothing

    conn.close

    setconn=nothing

    %>

    </body>

    </html>

    假设用户想要查看商品更具体的信息,那么就应该将商品图片做成一个超链接,连接到product.asp,在该页面显示具体信息。在该页面也有一个购买button,点击之后相同跳到buy.asp:

    <!--#includefile="inc/conn.asp"-->

    <body>

    <%

    pid=request.QueryString("id")

    response.Write(pid)

    sql="select*fromproductswherepid="&pid

    setrs=server.CreateObject("adodb.recordset")

    rs.opensql,conn,3,1

    ifnotrs.eofthen

    %>

    <tablewidth="387"height="119">

    <tr>

    <tdwidth="120"rowspan="4"><imgsrc="images/<%=rs(2)%>"width="100"height="100"></td>

    <tdwidth="276">名称:<%=rs(1)%></td>

    </tr>

    <tr>

    <td>价格:<%=rs(3)%></td>

    </tr>

    <tr>

    <tdheight="30"><%=rs(4)%></td>

    </tr>

    <tr>

    <tdalign="center"><inputtype="button"onclick="javascript:location.href='buy.asp?id=<%=rs(0)%>&price=<%=rs(3)%>'"value="购买"></td>‘点击购买之后触发一个onclick事件,跳转到buy.asp

    </tr>

    </table>

    <%

    endif

    rs.close

    setrs=nothing

    conn.close

    setconn=nothing

    %>

    </body>

    </html>

    用户点击了购买以后,将该商品的id传到buy.asp,以下来写buy.asp的代码:

    <!--#includefile="inc/conn.asp"-->‘将连接数据库的字符串包括进来

    <%

    pid=request("id")‘得到商品的id

    price=request("price")‘得到商品的价格

    sql="select*frommrcarwherepid="&pid&"andorderid='"&session("orderid")&"'"‘写sql语句来查询

    setrs=server.CreateObject("adodb.recordset")

    rs.opensql,conn,3,1

    假设用户所要购买的商品已经买过,那么就直接在原来的基础上加1,否则的话,就插入记录

    ifnotrs.eofthen

    sql="updatemrcarsetpcount=pcount+1wherepid='"&pid&"'andorderid='"&session("orderid")&"'"

    conn.executesql

    response.Redirect("mycar.asp")

    else

    sql="insertintomrcar(orderid,pid,price,pcount)values('"&session("orderid")&"',"&pid&","&price&",1)"

    conn.executesql

    response.Redirect("mycar.asp")

    endif

    rs.close

    setrs=nothing

    conn.close

    setconn=nothing

    %>

    运行完了sql语句之后将跳转到购物车页面(mycar.asp),显示出用户所购买的商品。而且有改动数量的button和删除的超链接

    <!--#includefile="inc/conn.asp"-->

    <%

    sql="selecta.*,b.pnamefrommrcara,productsbwhereorderid='"&session("orderid")&"'anda.pid=b.pid"

    setrs=server.CreateObject("adodb.recordset")

    rs.opensql,conn,3,1

    %>

    <formmethod="post"action="update.asp"name="form1">

    <tableborder="1">

    <tr><td>orderid</td>

    <td>商品名称</td>

    <td>商品id</td>

    <td>单位价格</td>

    <td>数量</td>

    <td>删除</td>

    <td>改动</td>

    </tr>

    <%

    sum=0

    i=1‘这里的i值到后边有介绍

    whilenotrs.eof‘将所购买的商品循环输出

    %>

    <tr><td><%=session("orderid")%></td>

    <td><%=rs("pname")%></td>

    <td><%=rs(1)%></td>

    <td><%=rs(2)%></td>

    <td><inputtype="text"size="10"value="<%=rs(3)%>"name="pcount"></td>

    <td><ahref="delete.asp?id=<%=rs(1)%>">删除</a></td>

    <td><inputtype="button"onclick="javascript:document.form1.action='update.asp?rowcount=<%=i%>&pid=<%=rs(1)%>';document.form1.submit();"value="确认改动"></td>

    </tr>

    <%

    i=i+1

    sum=sum+cint(rs(2))*rs(3)‘计算总价格

    rs.movenext

    wend

    rs.close

    setrs=nothing

    conn.close

    setconn=nothing

    %>

    </table>

    </form>

    <ahref="index.asp">返回继续购物</a>

    总金额为:<%=sum%>

    点击改动数量的button之后,就跳到update.asp:

    <!--#includefile="inc/conn.asp"-->

    <%

    这里须要注意在上个页面,有个i值,在这里就用到了,你要指明改动的是哪一行记录

    i=request.querystring("rowcount")

    pcount=request.form("pcount")(i)

    pid=request.querystring("pid")(i)

    sql="updatemrcarsetpcount="&pcount&"wherepid="&pid&"andorderid='"&session("orderid")&"'"

    conn.executesql

    conn.close

    setconn=nothing

    response.redirect("mycar.asp")‘运行完之后又一次跳转到mycar.asp

    %>

    相同点击删除之后,就跳到delete.asp,在该页面得到商品的id

    <!--#includefile="inc/conn.asp"-->

    <%

    pid=request.QueryString("id")

    response.write(pid)

    sql="deletefrommrcarwherepid="&pid&"andorderid='"&session("orderid")&"'"

    conn.executesql

    conn.close

    setconn=nothing

    response.Redirect("mycar.asp")‘运行完之后又一次跳转到mycar.asp

    %>一个简单的购物车做好了,它用到了连接数据库、数据库的增、删、改、查。

  • 相关阅读:
    Winform控件Enable=false显示优化
    request 报错The remote server returned an error: (415) Unsupported Media Type.
    InvalidArgument=Value of '1' is not valid for 'index'
    Redis学习笔记#12 Redis Cluster 集群
    centos7安装docker
    Redis学习笔记#11 关于key的建议
    Redis学习笔记#10 lua脚本,整合springboot调用
    ActiveMQ学习笔记#1
    SpringBoot学习笔记#2 具体化配置文件
    SpringBoot学习笔记#1 创建一个RESTful Web服务
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3870489.html
Copyright © 2011-2022 走看看