VB icon

A Basic Shopping Cart

Email
Submitted on: 1/6/2015 5:38:00 AM
By: Dustin R Davis (from psc cd)  
Level: Intermediate
User Rating: By 12 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 4567
 
     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!
				
'**************************************
' 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 (from psc cd)
'**************************************

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


 There are no comments on this submission.
 

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.