...

View Full Version : Cookies for shopping cart



wolfenr
10-27-2011, 10:35 AM
Hi and hope someone can help,

I'm doing some course work on setting / getting cookies for a shopping cart. It basically works except the user can only select one item of each of the goods on offer i.e. you can only buy one cup (not two), one saucer etc. etc.

I've deliberately not put a quantity field on the goods selection page and was hoping to modify my set cookie function so that if you've already selected one cup, the next tiume you select a cup, cups become 2 etc. etc.

Here's my set cookie function. Notice that I've hard wired a "1" in there which I want to change to the variable for the number of cups. I've also commented out some of the first few lines which were experiments to resolve the problem.


function setCookie(name)
{
alert("Thank you.\n\nYour basket has been updated.");
/* if(name.value !=0 || name.value==null)
name.value++;
x=getCookie(name) || 0;
alert(document.cookie);*/

var today = new Date();
var expiry = new Date(today.getTime()+28*24*60*60*1000); // plus 28 days
document.cookie=name+"=1;expires="+expiry.toGMTString();
cart = document.cookie;
}

My thanks,
R

A little while later...
I've cracked it, my function now looks like this and it appears to work...


function setCookie(name)
{
alert("Thank you.\n\nYour basket has been updated.");
x=parseInt(getCookie(name)) || 0;
alert(x);
y=x+1;
var today = new Date();
var expiry = new Date(today.getTime()+28*24*60*60*1000); // plus 28 days
document.cookie=name+"="+y+";expires="+expiry.toGMTString();
cart = document.cookie;
}

Thanks you fror your interest, appreciated,
R



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum