I am trying to write the basic code to make a shopping cart. But at the moment i am stuck, not knowing that much about programming (complete novice) I need to make a loop (possibly a for loop) so that the cookie does not keep getting overwritten every time something is selected. As only one item can go in the basket at the moment, and I would like to be able to multiple items. Please, if you have any suggestions, post them. The code so far is below.
<html>
<head>
<meta http-equiv=”Content-Language” content=”en-gb”>
<meta name=”GENERATOR” content=”Microsoft FrontPage 5.0″>
<meta name=”ProgId” content=”FrontPage.Editor.Document”>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<title>Shopping Cart</title>
<script>
function getCookie(NameOfCookie) {
// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.
if (document.cookie.length > 0) {
// Second we check to see if the cookie’s name is stored in the
// “document.cookie” object for the page.
// Since more than one cookie can be set on a
// single page it is possible that our cookie
// is not present, even though the “document.cookie” object
// is not just an empty text.
// If our cookie name is not present the value -1 is stored
// in the variable called “begin”.
begin = document.cookie.indexOf(NameOfCookie+”=”);
if (begin != -1) { // Note: != means “is not equal to”
// Our cookie was set.
// The value stored in the cookie is returned from the function.
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(“;”, begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null; // Our cookie was not set.
// The value “null” is returned from the function.
}
function setCookie(NameOfCookie, value, expiredays) {
// Three variables are used to set the new cookie.
// The name of the cookie, the value to be stored,
// and finally the number of days until the cookie expires.
// The first lines in the function convert
// the number of days to a valid date.
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24
// The next line stores the cookie, simply by assigning
// the values to the “document.cookie” object.
// Note the date is converted to Greenwich Mean time using
// the “toGMTstring()” function.
document.cookie = NameOfCookie + “=” + escape(value) + ((expiredays == null) ? “” : “; expires=” + ExpireDate.toGMTString());
}
function delCookie (NameOfCookie) {
// The function simply checks to see if the cookie is set.
// If so, the expiration date is set to Jan. 1st 1970.
if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + “=” +”; expires=Thu, 01-Jan-70 00:00:01 GMT”;
}
}
</script>
<meta name=”Microsoft Theme” content=”nature 011″>
</head>
<body>
<p align=”center”>
</p>
<p align=”center”><font size=”7″>Shopping Cart</font></p>
<script>
var basketcontents = getCookie(‘basket’); // get the cookie
basketarray = basketcontents.split(‘:’); // separate code, desc, price, qty
// do something to basketcontents
// …
alert(getCookie(‘basket’));
</script>
<script type=”text/javascript”>
document.write(basketarray[0]); // display code
</script>
</body>
<script language=”JavaScript”>
<!–
window.open = SymRealWinOpen;
//–>
</script>
<p align=”center”>
</p>
</P>
<center>
</font>
</font>
</font></font><table border=5 width=”430″>
<form action=”mailto:…” method=”post”>
<tr>
<td width=”118″ align=”center”><b>Product ID</b></td>
<td width=”212″ align=”center”><b>Description</b></td>
<td width=”78″ align=”center”><b>Price</b></td>
<td width=”78″ align=”center”><b>Quantity</b></td>
<tr>
<td align=”center”><script type=”text/javascript”>document.write( ‘<input type=”text” name=”prod” value=”‘ + basketarray[0] + ‘”>’)</script> </td>
<td align=”center”><script type=”text/javascript”>document.write( ‘<input type=”text” name=”desc” value=”‘ + basketarray[1] + ‘”>’)</script> </td>
<td align=”center”><script type=”text/javascript”>document.write( ‘<input type=”text” name=”price” value=”‘ + basketarray[2] + ‘”>’)</script> </td>
<td align=”center”><script type=”text/javascript”>document.write( ‘<input type=”text” name=”quant” value=”‘ + basketarray[3] + ‘”>’)</script> </td>
</tr>
</table>
<p align=”center”>
<b>Click here to email your order to <input type=”submit” value=”ButchBoy.co.uk”></b>
</p>
</form>
</P>
</body>
</html>