• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

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

  • 3
1 Solution
dispatch4599Author Commented:
This is the way my arrays are set.
function Product( id, name, price, desc, image )
	this.id = id;
	this.name = 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

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now