Using ASP to write shopping cart code

Source: Internet
Author: User
Tags rowcount

Online shopping has become the trend of life, after the online shopping, want to see what they have bought at any time, want to delete or change the number of a product, how to do it? I'll write the code and the explanation below. First to do the user landing page (login.asp):

<title> the realization of shopping cart </title>

<body>

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

<tablewidth= "border=" 1 ">

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

<tdwidth= "><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= "Login" ></td>

</tr>

</table>

</form>

</body>

Then write the message that receives the user to check for the existence of the user's page (check.asp), assuming that the user exists, then jump to the first page of the shopping (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

After the user login success, will username into the session, and assign an order to the user, like to go to the supermarket shopping push a shopping cart, this shopping cart belongs to you, with username and system time as the name of the shopping cart, and then jump to the shopping home.

Session ("name") =uname

Session ("OrderID") =time&uname

Response. Redirect ("index.asp")

Else

%>

<script>

Alert ("Username or password not true!");

History.back ();

</script>

<%

endif

Rs.close

Setrs=nothing

Conn.close

Setconn=nothing

%>

So, the next step is the home page of shopping (index.asp).

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

The following CSS style sheet is used to set the font in the table to 14th characters uniformly.

<styletype= "Text/css" > "

<!--

table{

font-size:14px;

}

-

</style>

<body>

<%

If the user does not log in directly to open the shopping home, then jump to the landing page

Ifsession ("name") = "Then

Response. Redirect ("Login.asp")

endif

%>

Make a search text box to facilitate users to search for products. The principle is this: After the user fills in the product to search, submits to this page through the form, searches the database to look like the user to fill the string the product, then displays

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

<inputtype= "text" size= "name=" "Search" id= "search"/><inputtype= "submit" value= "Search"/>

</form>

<%

Searchname=request. Form ("search") ' Get a user-filled string

sql= "Select*fromproductswherepnamelike '%" &searchname& "% '" ' Write SQL statements to find out the results

Setrs=server. CreateObject ("Adodb.recordset") ' Set a result set object

rs.opensql,conn,3,1

Whilenotrs.eof ' cyclic output of the product related information

%>

Each item below has a Buy button that needs to be made a form

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

<tablestyle= "Float:left" >

<tr>

<tdwidth= "103" rowspan= "3" ><ahref= "Product.asp?id=<%=rs (0)%>" > "width=" height= "border=" 0 "></a><inputtype=" hidden "name=" id "value=" <%=rs (0)%> " ><inputtype= "hidden" name= "Price" Value= "<%=rs (3)%>"/></td>

<tdwidth= "107" > Name:

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

</tr>

<tr>

<td> Price: <%=rs (3)%></td>

</tr>

<tr>

<tdalign= "Center" ><inputtype= "submit" name= "buy" value= "purchase"/></td>

</tr>

</table>

</form>

<%

Rs.movenext ' points to the next record

Wend

Rs.close

Setrs=nothing

Conn.close

Setconn=nothing

%>

</body>

Assuming that the user wants to see more specific information about the product, it should be a hyperlink to the product image, connected to the product.asp, which displays the specific information on that page. There is also a Buy button on this page, click after the same jump to 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= "rowspan=" 4 "> "width=" "height=" ></td>

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

</tr>

<tr>

<td> Price: <%=rs (3)%></td>

</tr>

<tr>

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

</tr>

<tr>

<tdalign= "Center" ><inputtype= "button" onclick= "javascript:location.href= ' buy.asp?id=<%=rs (0)%> &price=<%=rs (3)%> ' "value=" buy ></td> ' click on purchase to trigger an onclick event, jump to Buy.asp

</tr>

</table>

<%

endif

Rs.close

Setrs=nothing

Conn.close

Setconn=nothing

%>

</body>

After the user clicks on the purchase, the product's ID is passed to buy.asp, and the following code is written buy.asp:

<!--#includefile = "inc/conn.asp" and "include the string that connects the database in

<%

Pid=request ("id") ' Get the Product ID

Price=request ("price") ' Get the prices of the goods

Sql= "select*frommrcarwherepid=" &pid& "andorderid=" "&session (" OrderID ") &" ' "' Write SQL statements to query

Setrs=server. CreateObject ("Adodb.recordset")

rs.opensql,conn,3,1

If the user wants to buy the product has been bought, then directly on the original basis plus 1, otherwise, insert record

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

%>

After running the SQL statement, you will be redirected to the shopping cart page (mycar.asp), showing the items purchased by the user. And there are changes to the number of buttons and deleted hyperlinks

<!--#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> Product Name </td>

<td> Commodity id</td>

<td> Unit Price </td>

<td> Quantity </td>

<td> Delete </td>

<td> Changes </td>

</tr>

<%

Sum=0

I=1 ' I value here to the back has introduced

Whilenotrs.eof ' exports the purchased goods in a circular output

%>

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

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

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

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

<td><inputtype= "text" size= "ten" Value= "<%=rs (3)%>" name= "Pcount" ></td>

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

<td><inputtype= "button" onclick= "Javascript:document.form1.action= ' update.asp?rowcount=<%=i%> &pid=<%=rs (1)%> ';d ocument.form1.submit (); Value= "Confirm Changes" ></td>

</tr>

<%

I=i+1

Sum=sum+cint (RS (2)) *rs (3) ' Calculate Total price

Rs.movenext

Wend

Rs.close

Setrs=nothing

Conn.close

Setconn=nothing

%>

</table>

</form>

<ahref= "Index.asp" > Return to continue shopping </a>

Total amount is:<%=sum%>

After clicking on the number of buttons to change, jump to update.asp:

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

<%

It is important to note that in the previous page there is an I value, which is used here, and you want to indicate which line of records is changed

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") ' runs out and jumps to mycar.asp again

%>

After the same click Delete, jump to delete.asp, on the page to get the product 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") ' runs out and jumps to mycar.asp again

%> a simple shopping cart is done, it used to connect database, database of the increase, delete, change, check.

Using ASP to write shopping cart code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.