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.