Shopping Cart Woes... [RESOLVED]
Hello Again!
I am trying to create a shopping cart in ASP. I have been successful in that I can add single items to the shopping cart, however, I cannot seem to delete them from the shopping cart. Below is the code that I have for a delete item script, basically taken from asp101, but I can't seem to get it to work. Any suggestions?
HTML Code:
Sub DeleteItemFromCart()
Dim ItemID
ItemID = Request.QueryString("Key")
Set dctCart = Session("ShoppingCart")
If dctCart.Exists(ItemID) Then
dctCart.Remove dctCart(ItemID)
Else
Response.Write("Couldn't find that item.<br>")
End If
Set Session("ShoppingCart") = dctCart
End Sub
Thanks,
Jim P.
Re: Shopping Cart Woes...
Can you expand on "Can not Delete"? Do you get any error message or it is printing out "Couldn't find that item."? If thats the case make sure you are passing hte right Item ID, use response.write to print out the content of ItemID variable, e.g response.write ItemID.
Re: Shopping Cart Woes...
Yes, I have been using the response.write method to make sure the Key value is correct and it is....the output is that it cannot find the item...sorry about that.
Thanks,
Jim P.
Re: Shopping Cart Woes...
Quote:
Originally Posted by jpiller
Yes, I have been using the response.write method to make sure the Key value is correct and it is....the output is that it cannot find the item...sorry about that.
Thanks,
Jim P.
It seems like there is something wrong with the Exist method, is it possible to post the code of the Exist method? Without seeing the code it would be hard to tell what causing the problem.
Re: Shopping Cart Woes...
it explains the exists method here....
http://www.w3schools.com/asp/met_exists.asp
Jim P.
Re: Shopping Cart Woes...
The RemoveAll method works like a charm, but everytime I try and delete this item from the cart, it just can't find it????
Jim P.
Re: Shopping Cart Woes...
Quote:
Originally Posted by jpiller
The RemoveAll method works like a charm, but everytime I try and delete this item from the cart, it just can't find it????
Jim P.
Oh, you are using Dictionary Object, i thought it was an custom made object. Anyhow make sure you are adding the item with a key properly.
I would use a loop and display the item name and item key just to see if it actually exist. This will tell you whether the item actually exist in the dictionary object.
Hope this helps.
Re: Shopping Cart Woes...
there's no rule that says the key can't be all numbers is there? For example, could a key be 123456789?
Jim P.
Re: Shopping Cart Woes...
Quote:
Originally Posted by jpiller
there's no rule that says the key can't be all numbers is there? For example, could a key be 123456789?
Jim P.
As far as i know you can, I just tested it, it seems you can.
Here is my test Code :
VB Code:
<%
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "abc"
d.Add 134, "Test"
d.Add "b", "bbc"
dim a,k
a= d.items
k = d.keys
for i=0 to d.count-1
response.write "Key:" & k(i) & " Value:" & a(i) & "<br>"
next
%>
Re: Shopping Cart Woes...
okay, i've pretty much done my own version of the example on asp101. Here is the code that I have and I cannot for the life of me see the problem. The other issue that I'm having is that the cart won't add a quantity of a single item. In other words, if you run this code and add the same item over and over, it will add it as an individual item each time, which isn't supposed to happen. Any thoughts?
Thanks,
Jim P.
HTML Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!-- #include file="template.asp" -->
<%
Dim Cart
'Check to see if the cart exists. If not, then add one
If IsObject(Session("ShoppingCart")) Then
Set Cart = Session("ShoppingCart")
Else
Set Session("ShoppingCart") = Server.CreateObject("Scripting.Dictionary")
Set Cart = Session("ShoppingCart")
End If
'Add the item to the cart
Sub AddToCart(ItemID, ItemCount)
If Cart.Exists(ItemID) Then
Cart(ItemID) = Cart(ItemID) + ItemCount
Else
Cart.Add ItemID, ItemCount
End If
End Sub
'Remove the selected item from the cart. ***** NOT WORKING *****
Sub DeleteFromCart(ItemID, ItemCount)
Response.Write("This is the ItemID: " & ItemID & "<br>")
If Cart.Exists(ItemID) Then
If Cart(ItemID) <= ItemCount Then
Cart.Remove(ItemID)
Else
Cart(ItemID) = Cart(ItemID) - ItemCount
End If
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
'Just simply for error checking...all keys and values seem to be right.
Sub CheckKeys()
For Each Key in Cart
Response.Write("<br>The value for key " & Key & " is " & Cart(Key) & "<br>")
Next
End Sub
'Show the contents of the cart
Sub ShowCart()
Dim cnConn, rsGetDetails
Dim dblSubTotal
dblSubTotal = 0
Set cnConn = Server.CreateObject("ADODB.Connection")
cnConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("online_wizard.mdb")
cnConn.CursorLocation = 3
Set rsGetDetails = Server.CreateObject("ADODB.Recordset")
Response.Write("<table width='60%' border='1' align='center'>")
Response.Write("<tr bgcolor='#FFFFFF'>")
Response.Write("<th>ISBN # / Item #</th>")
Response.Write("<th>Book Title</th>")
Response.Write("<th>Quantity</th>")
Response.Write("<th>Price</th>")
Response.Write("<th>Delete Item</th>")
Response.Write("</tr>")
For Each Key in Cart
rsGetDetails.Open "SELECT * FROM tblBooks WHERE ISBN = '" & Key & "'", cnConn
Response.Write("<tr>")
Response.Write("<td>" & Key & "</td>")
Response.Write("<td>" & rsGetDetails("BookTitle") & "</td>")
Response.Write("<td align='center'><input type='text' size='2' name='ItemCount' value='1'></td>")
Response.Write("<td>" & FormatCurrency(rsGetDetails("ListPrice")) & "</td>")
dblSubTotal = dblSubTotal + rsGetDetails("ListPrice")
Response.Write("<td><a href='cart_functions.asp?Action=delete&Key=" & Key & "&ItemCount=1'>Delete Item</a>")
Response.Write("</tr>")
rsGetDetails.Close
Next
Response.Write("<tr>")
Response.Write("<td colspan='5' align='center'><a href='cart_functions.asp?Action=clear'>Empty All Items From Cart</a></td>")
Response.Write("</tr>")
Response.Write("</table>")
Response.Write("<br><br>Your Subtotal Is: " & FormatCurrency(dblSubTotal))
Set rsGetDetails = Nothing
cnConn.Close
Set cnConn = Nothing
End Sub
'Select the action based on what has been selected by the user
Select Case Request.QueryString("Action")
Case "add"
AddToCart Request.QueryString("ItemID"), "1"
Case "delete"
DeleteFromCart Request.QueryString("Key"), Request.QueryString("ItemCount")
Case "clear"
Cart.RemoveAll
End Select
'Show the cart no matter what.
ShowCart
%>
Re: Shopping Cart Woes...
Just tested your code, it seems like the problem is arising because you are using the key as a number. Add a charecter before the key e.g "ItemID" & Request.QueryString("ItemID"). That will resolve the problem.
VB Code:
Select Case Request.QueryString("Action")
Case "add"
AddToCart "ID" & Request.QueryString("ItemID"), "1"
Case "delete"
DeleteFromCart "ID" & Request.QueryString("Key"), Request.QueryString("ItemCount")
Case "clear"
Cart.RemoveAll
End Select
Re: Shopping Cart Woes...
Danial,
Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?
Thanks again for all your help.
Jim P.
Re: Shopping Cart Woes...
Quote:
Originally Posted by jpiller
Danial,
Thanks once again for all your help, but it still is not working. I changed the code as you suggested and now it's adding two "ID"'s at the beginning of the string and it's still not finding it...still says can't find that item in your cart. Could this be some kind of configuration problem with the server itself? I'm running win2k3 and IIS6. I wouldn't think that would be the problem, but you never know?
Thanks again for all your help.
Jim P.
No, its not working because your DeleteFromCart function is coded incorrectly. Here is the correct version. I have highlighted the secion in red. As you can see you need to convert a value to a number before you can do <= comparision. All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.
Hope this helps.
'
VB Code:
Remove the selected item from the cart. ***** NOT WORKING *****
Sub DeleteFromCart(ItemID, ItemCount)
Response.Write("This is the ItemID: " & ItemID & "<br>")
If Cart.Exists(ItemID) Then
[COLOR=Red]If cInt(Cart(ItemID)) <= cInt(ItemCount) Then[/COLOR]
Cart.Remove(ItemID)
Response.write "Item Removed"
Else
Cart(ItemID) = Cart(ItemID) - ItemCount
Response.write "Count Decreased"
End If
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
Re: Shopping Cart Woes...
Danial,
The script isn't working b/c the object that is in the cart isn't being seen by the Cart.Remove statement. It keeps saying that it doesn't find that object. The if statement you highlighted simply decides whether or not to delete all of that particular item or just the number specified by the user.
What I don't understand is why in the heck the item is showing up in the cart when I display it, it shows up when I loop through it, but when I go to delete the item, it doesn't see it. There must be something I'm doing wrong and I cannot figure out what it is....
Jim P.
Figured out the issue....
Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:
HTML Code:
Sub DeleteFromCart()
If Cart.Exists(Request.QueryString("Key")) Then
Cart.Remove Request.QueryString("Key")
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
The solution reads like this:
Sub DeleteFromCart()
If Cart.Exists(CStr(Request.QueryString("Key"))) Then
Cart.Remove CStr(Request.QueryString("Key"))
Else
Response.Write("Can't find that item in your shopping cart.")
End If
End Sub
When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.
Thanks Danial for all your help!
Jim P.
Re: Figured out the issue....
Quote:
Originally Posted by jpiller
Of all the stupid things....None of the documentation that I read and none of the examples that I worked with explained this, but here's the issue. In my original code, the DeleteFromCart Sub read like this:
When using Remove, your "Key" must be in string format, and b/c in ASP all variables are written as variant, I had to add the CStr function to convert it to a string.
Thanks Danial for all your help!
Jim P.
Quote:
Originally Posted by Danial
All variable in VBScript are Variant so you need to convert them to integer (cInt) or any other numerical form if you want to do numerical comparison.
Well I did kinda warn you about the variable type ;).
Yes its good to work with a strong typed, but in your case your Delete function you needed to convert the ItevCount to integer for it to work. You do no need to use cstr to convert it to a string.
Glad to know your problem is resolved.
Good luck and happy learning
:wave: