Important alert: (current site time 7/15/2013 10:57:07 PM EDT)
 

VB icon

A Basic Shopping Cart

Email
Submitted on: 5/11/2002 5:07:17 PM
By: Dustin R Davis  
Level: Intermediate
User Rating: By 12 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 44472
author picture
(About the author)
 
     This code is just to show you how you can make a simple quick and easy shopping cart for your site. Teaches you how to use session variables and the dictionary object (no, its not for spell checking either!)
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :A Basic Shopping Cart
'**************************************
Please do not steal code!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A Basic Shopping Cart
' Description:This code is just to show you how you can make a simple quick and easy shopping cart for your site. Teaches you how to use session variables and the dictionary object (no, its not for spell checking either!)
' By: Dustin R Davis
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7538&lngWId=4'for details.'**************************************

<%
'''''''''''''''''''''''''''''''''''''''''''''''''
' A Simple Shopping Cart						'
' Coded By: Dustin Davis						'
' Date: 05/11/2002								'
'												'
'This is just a simple example of how to start	'
'Shopping cart for your site. You can Add/Delete'
'and View your items							'
'												'
'this also shows you how to use Session			'
'variables and the dictionary object			'
'Please do not steal code, give credit where it	'
'is do!											'
'''''''''''''''''''''''''''''''''''''''''''''''''
Dim Basket' This will hold our shopping cart information
dim tmpItems ' This will hold all of the items we have in our Shopping Cart	
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This If statement will check to see if anything is in the querystring
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if Request.QueryString("action") = "" then
	
	'Write a form to take in information
	Response.Write "<HTML><BODY><FORM NAME='addit' ACTION='./Basket.asp?action=Add' METHOD='POST'>"
	Response.Write "<INPUT TYPE='TEXT' NAME='ITEM' VALUE=''><INPUT TYPE='SUBMIT' VALUE='ADD'></FORM>"
	Response.Write "</BODY></HTML>"
	
	'End the program so no further code will be executed
	Response.End 
end if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This If statement will check to see if add is in the querystring
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if Request.QueryString("action") = "Add" then
if IsObject(Session("ShopCart")) then	'Check to see if we have any previously saved session variables
	set Basket = Session("ShopCart")	'Since we do, we set that info to our Basket
else									'Else if we dont have anythign else saved,
	Set Basket = CreateObject("Scripting.Dictionary")	'Create a new dictionary object
end if
dim Cnt
Cnt = Basket.Count
do
	'if Basket.Exists(Cnt) then
		Cnt = Cnt + 1	
	'end if
	
loop until Basket.Exists("X" & Cnt) = false
Basket.Add "X" & Cnt, Request.Form("ITEM")		'Add and item to our basket, we take the total number
													'of items and add one
set Session("ShopCart") = Basket						'Save our session so we can use it later
tmpItems = Basket.Items									'Set tmpItems to hold all of our items in the basket
for i = 0 to Basket.Count - 1							'Loop to show whats currently in our basket
	
	'writes the value of the current item(i) and gives the option to delete it
	Response.Write i + 1 & ": " & tmpItems(i) & " - <a href='./Basket.asp?action=Del&Item=" & i & "'>Delete</a><BR>"
next
end if
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This If statement will check to see if del is in the querystring
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if Request.QueryString("action") = "Del" then
if IsObject(Session("ShopCart")) then	'Check to see if we have any previously saved session variables
	set Basket = Session("ShopCart")	'Since we do, we set that info to our Basket
else									'Else if we dont have anythign else saved,
	Set Basket = CreateObject("Scripting.Dictionary")	'Create a new dictionary object
end if
tmpKeys = Basket.Keys
on error resume next									'Use this for error checking
Basket.Remove tmpKeys(int(trim(Request.QueryString("Item"))))						'Remove the item
if err.number <> 0 then									'If error other than 0 
	Response.Write "Error " & err.number & "<BR>" & err.Description & "<P>"			'display error info
	Response.Write "QueryString Item: " & trim(Request.QueryString("Item")) & "<P>" 'Display 
end if
tmpItems = Basket.Items									'Set tmpItems to hold all of our items in the basket
set Session("ShopCart") = Basket						'Save our session so we can use it later
tmpItems = Basket.Items									'Set tmpItems to hold all of our items in the basket
for i = 0 to Basket.Count - 1							'Loop to show whats currently in our basket
	
	'writes the value of the current item(i) and gives the option to delete it
	Response.Write i + 1 & ": " & tmpItems(i) & " - <a href='./Basket.asp?action=Del&Item=" & i & "'>Delete</a><BR>"
next
end if
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/12/2002 2:41:34 PMDustin Davis

let me know if anything is wrong, or needs to be corrected, also, i am willing to give help to anyone who woudl like to go further with this
(If this comment was disrespectful, please report it.)

 
5/13/2002 10:45:24 PMEric Ross (*)

What would be the advantages of using the Dictionary object versus storing the data in a session or cookie array?
(If this comment was disrespectful, please report it.)

 
5/14/2002 11:25:40 AMDustin Davis

a dictionary IS an array, if you read in the MSDN about dictionaries, you will see, it explains why its better.
(If this comment was disrespectful, please report it.)

 
6/25/2002 11:47:22 AMM Kloc

I am new at this so please excuse me if this is a dumb question. I executed the code on my server and when I went to add
(If this comment was disrespectful, please report it.)

 
6/25/2002 11:49:24 AMM Kloc

I am new at this so please excuse me if
this is a dumb question. I executed
the code on my server and when I went
to add "test" I got an error message saying basket.asp was not found. Am I missing something? Thanks!
(If this comment was disrespectful, please report it.)

 
7/10/2002 9:37:11 AMOdytte

I just copy and paste your code, but then it displays
(If this comment was disrespectful, please report it.)

 
7/10/2002 9:42:31 AMOdytte

I just tried you're code, but then an error occured "The Page cannot be displayed". Please help me with this, thanks!
(If this comment was disrespectful, please report it.)

 
7/10/2002 10:51:36 AMDustin Davis

be more specific
(If this comment was disrespectful, please report it.)

 
7/11/2002 5:55:05 AMOdytte

Im new to this, then i just copy and pasted your code, but then the browser displays "The page cannot be displayed". So what should i do with this, please help me. Thanks.
(If this comment was disrespectful, please report it.)

 
8/2/2002 7:19:49 AMBen Hermer

To stop the page not found error rename basket.asp to the name of the page you have pasted the code into, ie my page is called shoppingcart.asp so in the if if then statement the line would read:

Response.Write
(If this comment was disrespectful, please report it.)

 
8/2/2002 7:21:04 AMBen Hermer

sorry not enough space, would read: Response.Write " "
(If this comment was disrespectful, please report it.)

 
8/2/2002 7:22:15 AMBen Hermer

Response.Write " " (what a hash I made of submitting this comment!!)
(If this comment was disrespectful, please report it.)

 
9/16/2002 6:28:56 AM

Great code!
I'll try to get the information into a DataBase. I have try some combinaties, but I can't find the code to get it to work.
I have try this code whit is the most succes full one:

for i = 1 to nNumCartItems
rs.addnew
rs("orderid") = OrderID
rs("productnumber") = CartItemNums(i)
rs("quantity") = CartItemQtys(i)
rs("price") = ItemPrice(i)
rs.update
next

I hope someone can help me.
TIA,
Jovana

When I use this I get it into the database, but it takes onlye the last item from the session.
If I have 3 deferent items, I get 3 new records, but al 3 the records are same as the last Item.
(If this comment was disrespectful, please report it.)

 
9/16/2002 11:06:56 AMDustin R Davis

have you tested the values in the session? Maybe they are all the same?
(If this comment was disrespectful, please report it.)

 
12/2/2002 5:13:20 AM

Hi,

I am trying your basket code and it works fine, for one form variable that is(ITEM). How can I modify the code so the basket can hold let's say 3 items which are displayed beside one another?
Or isn't this possible? I'm what you would call a beginner...
(If this comment was disrespectful, please report it.)

 
1/8/2003 1:09:04 PM

why can't you answer the question Dustin? - Perhaps it's the coding... hmmm... looks ultra-familiar.... -
Why R U dodging these easy questions? The answer is simple enough, especially for someone who wrote this code. - Oh wait. That answers it -
(If this comment was disrespectful, please report it.)

 
1/8/2003 1:17:04 PMDustin R Davis

Well, to elaborate on this situation, I dont answer these questions because i've moved on to other better things. This code was inspired by your messy speghetti code, but i cleaned it up and re-worked it in a fashion that people could use. It shows the basic use of a dictionary.
(If this comment was disrespectful, please report it.)

 
4/4/2003 6:12:13 AM

how do I use this I dont now please help if there is any more basic shopping cart code helps like this that mite help
(If this comment was disrespectful, please report it.)

 
8/6/2003 12:42:19 AM

great code, please show me also on how to add the quantity of that particular item. I'll appreciate if you send me your revised code. thank you in advance
(If this comment was disrespectful, please report it.)

 
8/12/2003 9:26:59 AM

why should i cut and paste the code....
please help me ....
then how to run this
(If this comment was disrespectful, please report it.)

 
10/3/2003 10:07:49 PM

i copy and paste this code but i don't understand it. Is it a process asp?? Can anyone send me their finished shopping cart asp with database?? I need it to save a life!!! My email is marson_007@hotmail.com... please put yr subject as shopping cart as i set my status to filter all junk mail. Thanks
(If this comment was disrespectful, please report it.)

 
5/24/2005 12:27:43 PM

I don't understand
(If this comment was disrespectful, please report it.)

 
4/25/2006 1:36:44 AMrajesh

fjksdjflsdkjfsdjfl;asd
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.