In E-commerce to achieve the method of shopping cart!! Reproduced

Source: Internet
Author: User
Tags implement modify setcookie split client
E-commerce | The realization of shopping cart trolley "function

The "trolley" is an important feature in the online shopping website (retail). This paper will explore the use of ASP program to achieve the "trolley" function skills. There are several ways to implement a "trolley" feature on a Web site, such as cookies, sessions, or hidden frames. Each of these methods has its advantages and disadvantages, and it focuses on using cookies to implement the "trolley" function.

Using cookies to achieve the advantages and disadvantages of the "trolley" function

Cookies are stored on the client computer via server-side CGI, scripting, or client script to make it easier for the server or client to use this information again. There are some functions in JavaScript that are specifically used to manipulate cookies, such as Setcookie (Name,value) that set the cookie value, Deletecookie (name) of the cookie, and so on (see source code). The advantage of using cookies to store "trolley" information is that even when a user accidentally closes the browser window, the information in the "cart" is not lost and it takes up very little server-side resources, and the disadvantage is that the client browser must be required to support cookies and open it.

Feature subkeys included with the "trolley"

1. Add the product to the "trolley", which is to order.

2. Delete the ordered goods from the "trolley".

3. Modify the quantity of a product in a "trolley".

4. Empty the "trolley".

5. Display, Statistics "trolley" in the merchandise.

Add a product to the "trolley"

Very simply, the information (name, price, quantity, discount, etc.) of a product is written to a cookie. There is no need to write all the information about the product to a cookie, just write down the number or ID of the item in the database (here with CATID) and the number of items. When the "trolley" information is displayed, all specific information about the product can be queried from the commodity database according to CATID. The following methods are implemented to add items to the "trolley":

function Addcat (catid,number) {//Add the item to the "Cart" function
if (number<=0) Deletecookie (″mycat″+catid); If the quantity of the product is less than or equal to 0, delete the product
else Setcookie (″mycat″+catid,catid+″,″+number); Write a Product ID and quantity to a cookie
var cat=window.open (″showcat.asp″,″cat″,″toolbar=no,menubar=no,location=no,status=no,width=420,height=280″); Open a new window to display the statistics of the product information, that is, "trolley"}

Remove the goods from the "cart"

That is, the product information in the cookie deleted, call Deletecat (CATID) can be.

function Deletecat (CATID) {
Deletecookie (″mycat″+catid); Remove from Cookie
var cat=window.open (″showcat.asp″,″cat″,″toolbar=no,menubar=no,location=no,status=no,width=420,height=280″); Display the statistical merchandise information in the Cat window, that is, refresh the display "trolley"}

Modify the number of items in the "trolley"

The principle is the same as adding the goods to the "trolley", except that the quantity of the goods is changed to the quantity now required.

Display and statistic information about the goods in the "trolley"

The display effect is implemented by showcat.asp. This uses the Request.Cookies collection in ASP. Request.Cookies is used to retrieve cookie values from customers. We retrieve the information of our "trolley" by traversing all cookies in the cookie collection, and then isolate the product ID and quantity. This uses the Split function in VBScript, which returns a one-dimensional array based on 0 that contains the specified number of substrings.

After taking out the commodity ID and the quantity of merchandise, the product ID is queried from the commodity database to find out the specific information of the commodity, and the name, unit price, discount, quantity and subtotal of the goods are finally exported, and the total number of merchandise and total amount of merchandise are accumulated.

The source code is as follows:

Showcat.asp
<body>
<form name=cat>
<table border= "1" width= "360" cellspacing= "0" cellpadding= "3" bordercolorlight= "#FFFFFF" bordercolor= "#008000" >
<tr>
&LT;TD width= "100%" colspan= "5" align= "center" ><b> Your Trolley information </b></td>
</tr>
<tr>
&LT;TD width= > Name </td>
&LT;TD width= > Price </td>
&LT;TD width= > Discount </td>
&LT;TD width= > Quantity </td>
&LT;TD width= > Subtotal </td>
&LT;TD width= "135" > </td>
</tr>

<% Dim Conn,rs,sqlstr,cook,catid,number,sum,allnum,i
I=0
Set Conn=server.createobject ("ADODB. CONNECTION ")" Set up an instance of database ADO connection
Conn.Open "DSN", "USERID", "PWD" to open a database connection
Set Rs=server.createobject ("ADODB. Recordset ")" Set up an instance of database ADO recordset connection

For each cook in Request.Cookies ' Traversal cookies Collection
If InStr (Cook, "Mycat") then ' judge whether the "trolley" information
Catid=split (Request.Cookies (Cook), ",") [0] ' isolate Product ID
Number=split (Request.Cookies Cook), ",") [1] ' isolate the number of goods
Sqlstr= "SELECT * from Cat where id=" "&catid&" ' SQL query statement
Rs.Open conn,sql,1,3 ' Execute query to produce recordset
Xiaoji=rs ("Unit Price") *rs ("discount") *number ' calculation subtotal

Response.Write "<tr><td width=45 ><font size=2 color= #800080 >" &rs ("name") & "</font> </td> "
Response.Write "<td width=45 ><font size=2 color= #800080 >" &rs ("Unit Price") & "</font></td>"
Response.Write "<td width=45 ><font size=2 color= #800080 >" &rs ("Discount") & "</font></td>"
Response.Write "<td width=45 ><input type=text name=number" &i& "value=" &number& "size=4> </td> "
Response.Write "<td width=45 ><font size=2 color= #800080 >" &xiaoji& "</font></td>"
Response.Write "&LT;TD width=135><input type=button value= update Onclick=addcat (" &rs ("ID") & ", Document.cat.number "&i&". Value) > <input type=button value= Delete onclick=deletecat ("&rs (" ID ") &") ></td></tr>

Allnum=allnum+number ' Cumulative total number of pieces of merchandise
Sum=sum+xiaoji ' Cumulative total amount
Rs.close



Related Article

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.