By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

java script shopping cart implementation with cookies

P: 1
Am trying to implement a shopping cart in java script which is using cookies.
I have a form consisting of itemID,quantity and price. these are details that the user has selected.
below is the code with two functions setcookie called from a button click event and getcookie called from link click event.But nothing is happening.Could anyone help me to figue what is the problem with the code.the others are comments after i discovered its not working,i was trying others.


Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. //global variables;
  3. var itemarray = new array
  4. itemarray[0]=itemid
  5. itemarray[1]=quantity
  6. itemarray[2]=price
  7.  
  8.  function setcookie(ccookie,itemarray);
  9. {
  10. document.cookie= ccookie + "="  + escape(itemarray);
  11. var scookie =ccookie;
  12. var itemID = new string(itemarray[0]);
  13. //document.cookie=scookie;
  14. var start= scookie.indexof(itemid + "&");
  15. If start>==0 {
  16. var end =scookie.indexof("",start+1)+1;
  17. if (itemarray[1] >0) {
  18.  
  19.         sCookie = sCookie.replace(sCookie.slice(Start,End), itemID + & + itemarray[1] + "& "itemarray[2] "");
  20.  
  21.               } 
  22.     else {
  23.  
  24.         //new item quantity  is 0 so remove it from basket
  25.  
  26.  
  27.         sCookie = sCookie.replace (sCookie.slice(Start,End), );
  28.  
  29.           }
  30.  
  31. }
  32.  
  33. else {
  34.  
  35. //item not in basket already
  36.  
  37.   sCookie = sCookie.concat(itemID + &, itemarray[1] + "&: itemarray[2] );
  38.  
  39. }
  40.  
  41. return sCookie;
  42.  
  43. }
  44.  
  45. Functio getcookie(scookie)
  46. {
  47. //var rcookie = document.cookie;
  48. if (document.cookie.length>0)
  49.   {
  50.   c_start=document.cookie.indexOf(scookie + "=")
  51.   if (c_start!=-1)
  52.     { 
  53.     c_start=c_start + scookie.length+1 
  54.     c_end=document.cookie.indexOf(";",c_start)
  55.     if (c_end==-1) c_end=document.cookie.length
  56.     return unescape(document.cookie.substring(c_start,c_end))
  57.     } 
  58.   }
  59. return ""
  60.  
  61. //var start = rcookie.indexof(itemid + "&");
  62. //var end =rcookie.indexof("&",start+1);
  63. //var citem =rcookie.slice(start,--end);
  64. //var itemarray=citem.split("&");
  65. //return itemarray
  66. //}
  67. </script>
Apr 20 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
See how you can write cookie code. JavaScript is case-sensitive - you can't mix and match.
Feb 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.