Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    43
    Thanks
    18
    Thanked 1 Time in 1 Post

    Cookies for shopping cart

    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.

    Code:
      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...

    Code:
    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
    Last edited by wolfenr; 10-27-2011 at 09:49 AM. Reason: Resolved


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •