Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Madness

Status
Not open for further replies.

Jouster

Programmer
Dec 4, 2000
82
US
I'm trying to do a simple shopping cart and am having problems with the following code. All I'm trying to do is read in the cookie with the items in it and add them to an array. Then add the new item to the array and re-write the cookie.

The problem is i never get the items I wrote to the cookie if I do a for each loop. Any help would be appreciated.

<%

dim cartItems()
dim i,j

i=0
if Request.Cookies(&quot;Cart&quot;).Haskeys = true then
For Each objCK In Request.Cookies(&quot;Cart&quot;)
redim Preserve cartItems(i)
cartItems(i)=Request.Cookies(objCK)
i = i + 1
Next
end if

If Request.QueryString(&quot;ProdCode&quot;) <> &quot;&quot; then
redim Preserve cartItems(i)
cartItems(i) = Request.QueryString(&quot;ProdCode&quot;)
i = i + 1
end if

if i <> 0 then
for j = 0 to (i-1)
Response.Cookies(&quot;Cart&quot;)(&quot;Item&quot; & j) = cartItems(j)
next
end if
Response.Redirect(&quot;cuCart.asp?ProdCode=&quot;&Request.QueryString(&quot;ProdCode&quot;) )
%>
 
Have you tried dumping trace data out to the HTML page to see what is happening at different steps in your code?

Code:
    if Request.Cookies(&quot;Cart&quot;).Haskeys = true then
Response.Write &quot;<!-- TRACE: Cart.Haskeys &quot;
        For Each objCK In Request.Cookies(&quot;Cart&quot;)
               redim Preserve cartItems(i)
Response.Write &quot;Redim cartItems &quot; & i
            cartItems(i)=Request.Cookies(objCK)
Response.Write &quot;cartItem(&quot; & i & &quot;): &quot; + cartItems(i)
            i = i + 1
        Next
Response.Write &quot; -->&quot;    
    end if

-pete
 
Thanks, I finally got a chance to try this and I seem to be having a problen in the &quot;for each loop&quot;. I cannot iterate through the items in the cookie. I have looked the code over and over and looked at a few examples and I can't figure out what is wrong. Any other suggestions?

Thanks,
Rick
 
Out of curiosity, does the Request.Cookies(&quot;Cart&quot;) cookie holds keynames for other cookies or are these the actual values you want to add to your array?
When you do the for each it is assigning each value from the collection to your objCK variable, but then you turn around and request a cookie with the name that was just assigned to the objCk variable, so I was a little confused.

If you were just trying to put each part of the Cart cookie into the array, then you don't need to do Request.Cookies(objCK) because the value your looking to add to your array has been assigned to objCk, so you can simply assign the array to the object.

If objCk is actually just supposed to be a string that is the name of another cookie, print them out with brackets around them or something like that, it may be possible your picking up some spaces that you need to trim.

-Tarwn 01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Thanks for answering Tarwn.

My thinking on this is I would have a cookie called Cart and in this cookie I would have the keys Item1,Item2 ect. and the values for the keys.

So when I first enter my page I need to see if there are any items in the cart(cookie). If there are then I want to read each keys value and add it to my array. Next I would append the new item to the end of the array. Next rewrite the cookie with the old and new item(s).

This is my first use of cookies and am not sure if I am violating a rule that I'm not aware of or something.

The &quot;for each&quot; loop I'm using is from an example I found on the web and that is the way they used the objCK variable so I'm really not sure if its right or wrong.

Sincerely,
Rick
 
Update.... I broke down and purchased an ASP book and figured out my problem with the &quot;for each&quot; loop.

I replaced the line : cartItems(i)=Request.Cookies(objCK)

with : cartItems(i)=Request.Cookies(&quot;Cart&quot;)(objCK)

and was able to retrieve my information.
Thanks to everyone who tried to help.

Rick
 
No problem, we're glad you found a solution. Thanks for reposting it for later readers :)

-Tarwn 01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top