brianindium
asked on
Setting Up Cookies For Shopping Cart
All right here is the problem.
Trying to creat a shopping page that has three products.
I want the cookies to store the product name and a running quantity.
On "Add Item" button, it needs to check if the cookie is there.
If cookie is not there, create cookie with a value (quantity) of 1.
If cookie is present and 1 to current value (quantity).
I am having two problems with current code.
Problem 1:
The code I am using now writes over present cookie value with new value.
Ex.
Click on "Product 1". Cookie is added for Product 1 with quanity of 1.
Next.
Click on "Product 2". Cookie is overwritten. Product 1 information is lost. Prosuct 2 information is written.
Problem 2:
Values (Quantity) is added together as strings instead on integers. I have tried parseInt() and parseFloat() in various places with no luck.
Ex.
Click on "Product 1". Cookie is added for Product 1 with quanity of 1.
Next.
Click on "Product 1" again. Cookie is added for Product 1 with quanity of 11.
Next.
Click on "Product 1" again. Cookie is added for Product 1 with quanity of 111.
Etc.
Here is the HTML page and code I am using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JavaScript Cookies - Widgets</title>
<SCRIPT LANGUAGE="JavaScript">
var date = new Date("December 31, 2023");
var cookieDate = date.toGMTString();
function addItem(itemName) {
var cookieContent;
var counterValue = readCookie(itemName);
//this is where I have been trying the parseInt, parseFloat
quantity = counterValue;
quantity = quantity + 1;
// I had to do this because on every click of the same item it would add a : Ex. Product1::11
cookieContent = itemName + ":";
document.cookie = cookieContent + quantity + ";expires=" + cookieDate + ";";
function readCookie(itemName) {
var docCookies = document.cookie;
var startIndex = docCookies.indexOf(itemNam e);
if (startIndex == -1) {
cookieContent = itemName + ":";
document.cookie = cookieContent + "1" + ";expires=" + cookieDate + ";";
};
startIndex += itemName.length + 1;
var endIndex = docCookies.indexOf(";",sta rtIndex);
if (endIndex == -1) endIndex = docCookies.length;
var cookieValue = docCookies.substring(start Index, endIndex);
return cookieValue;
}
}
</SCRIPT>
</head>
<body>
<h3>Product 1</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<a href='javascript:addItem(" Product1") '>Put In Shopping Cart</a><br>
<br>
<h3>Product 2</h3>
<p> Price - $150.00, Shipping - $ 10.95</p>
<a href='javascript:addItem(" Product2") '>Put In Shopping Cart</a> <br>
<br>
<h3>Product 3</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<p><a href='javascript:addItem(" Product3") '>Put In Shopping Cart</a> </p>
<p><a href="javascript:alert(doc ument.cook ie);">View Cookies</a></p>
</body>
</html>
I want to write the cookie something like this:
Product1:2;Product2:1;Prod uct3:4;
Meaning: Product1 has quantity of 2; Product2 has quantity of 1; Product3 has quantity of 4
On the cart page I will try and parse out each item with a quantity value for each and then assign price * quanity for a total.
Sorry, all I have to give is 125 points.
Thanks in advance for any help.
Trying to creat a shopping page that has three products.
I want the cookies to store the product name and a running quantity.
On "Add Item" button, it needs to check if the cookie is there.
If cookie is not there, create cookie with a value (quantity) of 1.
If cookie is present and 1 to current value (quantity).
I am having two problems with current code.
Problem 1:
The code I am using now writes over present cookie value with new value.
Ex.
Click on "Product 1". Cookie is added for Product 1 with quanity of 1.
Next.
Click on "Product 2". Cookie is overwritten. Product 1 information is lost. Prosuct 2 information is written.
Problem 2:
Values (Quantity) is added together as strings instead on integers. I have tried parseInt() and parseFloat() in various places with no luck.
Ex.
Click on "Product 1". Cookie is added for Product 1 with quanity of 1.
Next.
Click on "Product 1" again. Cookie is added for Product 1 with quanity of 11.
Next.
Click on "Product 1" again. Cookie is added for Product 1 with quanity of 111.
Etc.
Here is the HTML page and code I am using:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JavaScript Cookies - Widgets</title>
<SCRIPT LANGUAGE="JavaScript">
var date = new Date("December 31, 2023");
var cookieDate = date.toGMTString();
function addItem(itemName) {
var cookieContent;
var counterValue = readCookie(itemName);
//this is where I have been trying the parseInt, parseFloat
quantity = counterValue;
quantity = quantity + 1;
// I had to do this because on every click of the same item it would add a : Ex. Product1::11
cookieContent = itemName + ":";
document.cookie = cookieContent + quantity + ";expires=" + cookieDate + ";";
function readCookie(itemName) {
var docCookies = document.cookie;
var startIndex = docCookies.indexOf(itemNam
if (startIndex == -1) {
cookieContent = itemName + ":";
document.cookie = cookieContent + "1" + ";expires=" + cookieDate + ";";
};
startIndex += itemName.length + 1;
var endIndex = docCookies.indexOf(";",sta
if (endIndex == -1) endIndex = docCookies.length;
var cookieValue = docCookies.substring(start
return cookieValue;
}
}
</SCRIPT>
</head>
<body>
<h3>Product 1</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<a href='javascript:addItem("
<br>
<h3>Product 2</h3>
<p> Price - $150.00, Shipping - $ 10.95</p>
<a href='javascript:addItem("
<br>
<h3>Product 3</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<p><a href='javascript:addItem("
<p><a href="javascript:alert(doc
</body>
</html>
I want to write the cookie something like this:
Product1:2;Product2:1;Prod
Meaning: Product1 has quantity of 2; Product2 has quantity of 1; Product3 has quantity of 4
On the cart page I will try and parse out each item with a quantity value for each and then assign price * quanity for a total.
Sorry, all I have to give is 125 points.
Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are a pimp and a scholar.
Thanks.
Thanks.
<h3>Product 1</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<a href='javascript:addItem("
<br>
It was
<h3>Product 1</h3>
<p>Price - $150.00, Shipping - $10.95</p>
<a href='javascript:addItem("
<br>