Arrays and cookies for Shopping Cart

I am working on a homework assignment in which I need to create a "store".  The first time we did this assignment we needed to pass parameters  and/or use query strings to display the products, the price of the products, and description as well as collect the payment information for the checkout.  My question is this.   If I have each product (there is a total of 4) in their own array according to (product image, product name, description, price), do I need to display my products and I previously did or use cookies?  I am really confused on this topic and probably shouldn't be.  Also, on the form, how to I get, set, delete, cookies.  If someone could show me a working example and tell me how it all works, I would be greatly appreciative.  I really want to learn this material.

	<div id="content">
		<h1>Firecrackers Specials</h1>
		<h2> Order your Firecrackers today! </h2>

		<form onsubmit="return checkout( this );" method="get" action="checkout.html">
				<tr><th>Add to Order</th><th><!-- picture --></th><th>Name</th><th>Price</th><th>Description</th></tr>
				<script language="javascript" type="text/javascript">
					<!-- start hiding
					for( var x = 0 ; x < productArray.length ; x++ ) {
						document.writeln( '<tr>' );
						document.writeln(  '<td><input type="checkbox" name="product" value="' + x + '"/></td>' );
						document.writeln( '<td><img alt="[ ' + productArray[ x ].name + ' ]" src="images/' + productArray[ x ].image + '" width="150"/></td>' );
						document.writeln( '<td>' + productArray[ x ].name + '</td>' );
						document.writeln( '<td>' + formatCurrency( productArray[ x ].price ) + '</td>' );
						document.writeln( '<td>' + productArray[ x ].desc + '</td>' );
						document.writeln( '</tr>' );
					// stop hiding-->
			<input type="submit" value="Checkout"/>

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dispatch4599Author Commented:
This is the way my arrays are set.
function Product( id, name, price, desc, image )
{ = id; = name;
	this.price = price;
	this.desc = desc;
	this.image = image;
var productArray = new Array( );

productArray[ 0 ] = new Product( 0, "this product", 5.00,"awesomeness.","product01.jpg" );
productArray[ 1 ] = new Product( 1, "another product", 6.50,"even more awesome","product02.jpg" );
productArray[ 2 ] = new Product( 2, "yet another product", 7.00,"Total awesome","product03.jpg" );
productArray[ 3 ] = new Product( 3, "last product", 6.50,"UNBELIEVEABLE","product04.jpg" );

function getProduct( id ) {
	//alert( "looking for product" + id );
	for( var x = 0 ; x < productArray.length ; x++ ) {
		//alert( "cheching " + productArray[ x ].id );
		if( id == productArray[ x ].id ) {
			//alert( "product " + id + " found" );
			return productArray[ x ];	

Open in new window

If you are trying to build a shopping cart it would be more advisable to handle the products in a session on the server side and when the session is about to be closed you can persist the cart details so that you can retrieve it when the user logins in the next time.

But if you are looking to handle this through cookies then here is the link for cookie manipulation

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dispatch4599Author Commented:
I guess I'm making this a lot harder than it needs to be and for the life of me I am beating myself up over it.  This is the explanation given by my professor.  

 "I would just use one cookie and then I would put a string into it that was a comma separated list of product id’s that the user has added to their cart.  So for example I might have this in a cookie:


Meaning that they have added 1 product 1, 2 product 4’s, and 1 product 9 to their cart.

If you do it this way then whenever the user adds something to the cart you retrieve the string stored in the cookie and append the new product number to the end of it.  When they want to remove a product you remove the id for that product from the string and when you need to display the list you just display all of the products according to the id’s that are in the string."

Previously we passed parameters and now we need to use cookies.  I don't need the code, just some guidance.  How do I get "this id" from my object? Yes, I am brain fried.  Seems everything has just gone out of my mind since trying to get this to work for me.

dispatch4599Author Commented:
never mind.. I think my eyes are opening up.  Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.