/    Sign up×
Community /Pin to ProfileBookmark

Cookie help needed

Hey guys, I am currently working an website as a final project for one of my college classes.

Right now the site simply lists 15 products to sell. All my products are on separate pages.

The current assignment is to make a simple “shopping cart” by adding quantity text box to each product and recording it in a cookie along with product number after the user clicks add to cart button. The product numbers are preset and mine are 0001-0015.

Once the user hits check out button, the alert box should show all added products (quantity and product number for each).

After searching the internet for a couple of days I really can’t figure out how to do this. Currently one of my pages is using this template code that sets a cookie with quantity and product number and shows it in an alert box, but its really nowhere near what it is supposed to do

<html>
<head>
<script type=”text/javascript”>
<!–
function WriteCookie()
{
if( document.myform.customer.value == “” )
{
alert(“Enter some value!”);
return;
}

cookievalue= escape(document.myform.customer.value) + “;”;
document.cookie=”Quantity=” + cookievalue;
alert(“Setting Cookies : ” + “product #0015”);
}

function ReadCookie()
{
var allcookies = document.cookie;
alert(“All Cookies : ” + allcookies );

// Get all the cookies pairs in an array
cookiearray = allcookies.split(‘;’);

// Now take key value pair out of this array
for(var i=0; i<cookiearray.length; i++){
name = cookiearray[i].split(‘=’)[0];
value = cookiearray[i].split(‘=’)[1];
alert(“Key is : ” + name + ” and Value is : ” + value);
}
}
//–>
</script>
</head>
<body>
<form name=”myform” action=””>
Enter name: <input type=”text” name=”customer”/>
<input type=”button” value=”Add to Cart” onclick=”WriteCookie();”/>
<input type=”button” value=”Check Out” onclick=”ReadCookie()”/>
</form>
</body>
</html>

I would greatly appreciate any help, and thanks in advance.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@wbportMar 02.2012 — Did you find out anything when you searched this forum for [B]cookies[/B]? Is there anything important in a cookie other than its name and value?
×

Success!

Help @nik2304 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...