473,749 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java script shopping cart implementation with cookies

1 New Member
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
1 6496
acoder
16,027 Recognized Expert Moderator MVP
See how you can write cookie code. JavaScript is case-sensitive - you can't mix and match.
Feb 4 '08 #2

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

Similar topics

3
2904
by: Robert | last post by:
Hello all.. Im a beginner in php and looking to do something similar to a shopping cart system. I want users to be able to view profiles on people and be able to add them to the "shopping cart" until they decide they are ready to send a mass email to all the people in their cart. They will be able to view the people in their cart, add and remove as desired, and then simply fill out a form and the message will be mailed to all those in...
1
2094
by: Stuart | last post by:
Can anyone recomend a good shopping cart script or an example of how to make one. I'm currently using http://www.mals-e.com free shopping cart script that they host. unfortunatly because they host the cart themselves I can't hack around and customise the scripts the way I want. the linking method I currently use is: <FORM name="form1" action="http://ww7.aitsafe.com/cf/add.cfm" method="post">
4
2270
by: Jason Burr | last post by:
I was wondering what the best approach to handling a shopping cart was. My concerns 1. Cookies not accepted (session variables not maintained and setting retreiving cookies doesn't work) 2. Use an IP (people behind firewalls or using NAT or other subrouting appear as same client) 3. Use of GET (people can bookmark cart contents then return and an order has been fuffilled but shows as new because contents of string are bookmarked too)
1
1822
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
5
1665
by: zaw | last post by:
Hi I am working on implementing this script to shopping cart. Basically, it copies fill the shipping address from billing automatically. I believe one or more syntax is not netscape compatible. Can anyone point out which one it is and how to make it both netscape and MS browser compatible? I hope if I can make the script compatible for those two at extreme, it will probably work with most browser out there. As you would notice, this form...
1
3228
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID in table. All products and cart status pages are on non SSL connection. On checkout to get secure user information I shifted connection to SSL but when shifting to SSL, the SessionID changed (As is this is default behavior of IIS to prevent...
7
2635
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would like it to work properly so that a record count counts through all the books and genertates these numbers. watch out for line breaks <%@ Language=VBScript %> <% Option Explicit %> <!--#include file="DatabaseConnect.asp"-->
3
4431
by: Milsnips | last post by:
Hi there, i had a small project where i used a session to store the shopping cart, but if it times out, the user loses the cart. Then i thought instead of using session, i'll store the cart in a cookie, but cookie only stores string values, which makes it tricky to update/remove cart item values because i'd have to so substring, split, etc... What i'm thinking now is to store the cart in a database table, and just add a cookie with...
3
2894
by: Paulo | last post by:
Hi, beginner on asp.net 2.0 C# VS 2005, how can I use the shopping cart concept on my application? When the user clicks add item, it will be stored on some storage format, I dont know what is the term: temp/global DataSet, etc... and it should be read on other web-form to checkout the items... Can you point me to the right direction? Thank you very much!
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.