SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Manchester
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Writing to cookie not working

    I am trying to convert a framed shopping cart to non frames but the cookie does not appear to be writing properly and I am sure it is a simple answer as it works fine in frames but am tearing my hair out. Sorry for it being a long one but would really appreciate if someone could help. When I view the basket the Item number is coming up fine but then saying object expected so presume it isn't writing the other fields to the cookie.
    The window alert shows the correct values, just not seem to be storing the details properly

    Additem code is..

    function addItem(newItem, newPrice, newQuantity, newNumber, newWeight) {
    if (newQuantity >= 1000) {
    rc = alert('<%=txt_wrong_quantity%>!');
    } else {
    if (newQuantity >= 1) {
    window.alert(''+newQuantity+' x '+newItem+' <%=txt_add_to_cart%>!');

    index = document.cookie.indexOf("ShoppingBasket");
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1) {
    countend = document.cookie.length;
    }
    document.cookie="ShoppingBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newWeight+"|"+newQuantity+"]";
    }
    }
    }

  2. #2
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I created this test page and it is adding to the cookie just fine in IE6:

    Code:
    <html>
    <head>
      <script>
    
      function addItem(newItem, newPrice, newQuantity, newNumber, newWeight) {
    
        if (newQuantity >= 1000) {
          rc = alert('Wrong Quantity!');
        }
        else {
          if (newQuantity >= 1) {
            window.alert(''+newQuantity+' x '+newItem+' Added to Cart!');
    
            index = document.cookie.indexOf("ShoppingBasket");
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
              countend = document.cookie.length;
            }
    
          document.cookie="ShoppingBasket="+document.cookie.substring(countbegin, countend)+"["+newItem+"|"+newPrice+"|"+newNumber+"|"+newWeight+"|"+newQuantity+"]";
          }
        }
      }
      </script>
    </head>
    <body>
      <a href="#" onclick="addItem('123', 23.99, 45, '123Number', '123Weight');">Add 123 to Cart</a>
      <br>
      <a href="#" onclick="addItem('XYZ', 23.99, 45, 'XYZNumber', 'XYZWeight');">Add XYZ to Cart</a>
      <br>
      <a href="#" onclick="addItem('ABC', 23.99, 45, 'ABCNumber', 'ABCWeight');">Add ABC to Cart</a>
      <br>
      <a href="#" onclick="addItem('789', 23.99, 45, '789Number', '789Weight');">Add 789 to Cart</a>
      <br>
      <a href="#" onclick="alert(document.cookie);">Show Cookie</a>
    </body>
    </html>
    Charles

  3. #3
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, you didn't say if you were having a problem with the cookie being save after you closed the browser, but that's the only thing I can see that could be causing you a problem.

    To fix this you have to assign an expiration date to the cookie. I'm assigning a date very far in the future, but if you wanted the cart to expire after 30 days you could calculate the appropriate date.

    Here's the end result:
    Code:
    <html>
    <head>
      <script>
      function setCookie(name, value, expires, path, domain, secure) {
          document.cookie= name + "=" + value +
              ((expires) ? "; expires=" + expires.toGMTString() : "") +
              ((path) ? "; path=" + path : "") +
              ((domain) ? "; domain=" + domain : "") +
              ((secure) ? "; secure" : "");
      }
    
      function addItem(newItem, newPrice, newQuantity, newNumber, newWeight) {
        var cookieValue ;
        var expirationDate = new Date("December 31, 2040") ;
    
        if (newQuantity >= 1000) {
          alert('Wrong Quantity!');
        }
        else {
          if (newQuantity >= 1) {
            window.alert('' + newQuantity + ' x ' + newItem + ' Added to Cart!');
    
            cookieValue = getCookie("ShoppingBasket");
    
            if (cookieValue) {
              cookieValue += "[" + newItem + "|" + newPrice + "|" + newNumber + "|" + newWeight + "|" + newQuantity + "]";
            }
            else {
              cookieValue = "[" + newItem + "|" + newPrice + "|" + newNumber + "|" + newWeight + "|" + newQuantity + "]";
            }
    
            setCookie("ShoppingBasket", cookieValue, expirationDate)
          }
        }
      }
      </script>
    </head>
    <body>
      <a href="#" onclick="addItem('123', 23.99, 45, '123Number', '123Weight');">Add 123 to Cart</a>
      <br>
      <a href="#" onclick="addItem('XYZ', 23.99, 45, 'XYZNumber', 'XYZWeight');">Add XYZ to Cart</a>
      <br>
      <a href="#" onclick="addItem('ABC', 23.99, 45, 'ABCNumber', 'ABCWeight');">Add ABC to Cart</a>
      <br>
      <a href="#" onclick="addItem('789', 23.99, 45, '789Number', '789Weight');">Add 789 to Cart</a>
      <br>
      <a href="#" onclick="alert(document.cookie);">Show Cookie</a>
    </body>
    </html>
    This cookie will persist even when the browser is closed and re-opened.

    Charles

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Manchester
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, amazing response speed. Many thanks for taking the time to look at this. The problem I have is that when I view the cart it only sees the item number and then gives me an error saying object expected which is why I thought it wasn't saving the onclick details to teh cookie..Could you add this to your little test page and see if you can get it to work?

    Many thanks in advance....

    The show basket code is

    function showBasket() {
    index = document.cookie.indexOf("ShoppingBasket");
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1) {
    countend = document.cookie.length;
    }
    fulllist = document.cookie.substring(countbegin, countend);
    subtotal = 0;
    document.writeln('<CENTER><FORM NAME="updateform"><font size="5"><b><%=txt_cart%></B></FONT><HR WIDTH=75% size=1 COLOR="#000000">');
    document.writeln('<table cellpadding="2" cellspacing="1" border="0">');

    document.writeln('<TR><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_qty%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_code_no%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_item%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_price%>&nbsp;<%=txt_currency%></b></FONT></TD><td BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_total%></b></FONT><TD BGCOLOR="#B3B3B3"><b><FONT COLOR="#B3B3B3">.</FONT></b></TD></TR>');
    itemlist = 0;
    for (var i = 0; i <= fulllist.length; i++) {
    if (fulllist.substring(i,i+1) == '[') {
    itemstart = i+1;
    thisitem = 1;
    } else if (fulllist.substring(i,i+1) == ']') {
    itemend = i;
    thequantity = fulllist.substring(itemstart, itemend);
    itemtotal = 0;
    itemtotal = (eval(theprice*thequantity));
    temptotal = itemtotal * 100;
    subtotal = subtotal + itemtotal;
    itemlist=itemlist+1;
    document.write('<tr><td align=middle BGCOLOR="#D9D9D9"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3></td><td BGCOLOR="#D9D9D9"><font size="-2"><a href=item_show2.asp?code_no='+thenumber+'>'+thenumber+'</A></FONT></td>');

    document.write('<td align=left BGCOLOR="#D9D9D9"><font size="-2"><a href=item_show.asp?code_no='+thenumber+'>'+theitem+'</A></FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2">'+theprice+'</FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2">'+alterError(itemtotal)+'</FONT></td><td WIDTH="38" align=right BGCOLOR="#D9D9D9"><a href="javascript:updateItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><IMG SRC="images/update.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_update%>"></a><IMG SRC="images/space.gif" WIDTH="3" HEIGHT="2" ALT=""><a href="javascript:removeItem('+itemlist+')"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_remove%>"></a>&nbsp;</td></tr>');

    } else if (fulllist.substring(i,i+1) == '|') {
    if (thisitem==1) theitem = fulllist.substring(itemstart, i);
    if (thisitem==2) theprice = fulllist.substring(itemstart, i);
    if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
    if (thisitem==4) theweight = fulllist.substring(itemstart, i);
    thisitem++;
    itemstart=i+1;
    }
    }

    document.writeln('<tr><td align=right b><%=txt_product_total%>&nbsp;<%=txt_currency%></b></FONT></td><td align=right font size="-2">'+alterError(subtotal)+'</FONT></td><td BGCOLOR="#B3B3B3"><FONT COLOR="#B3B3B3">.</FONT></td></tr>');
    document.writeln('</TABLE><CENTER><font size="1"><IMG SRC="images/update.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_update%>"> = <%=txt_update%> <IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_remove%>"> = <%=txt_remove%></FONT></CENTER>');
    document.writeln('</FORM>');
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Manchester
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, the script to call this is

    <script LANGUAGE="JavaScript">
    showBasket();
    </script>

  6. #6
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this. The problem was your altererror function which didn't exist anywhere.

    I also changed to code to update as you add instead of requiring a refresh of the page.

    Code:
    <html>
    <head>
      <script>
      function setCookie(name, value, expires, path, domain, secure) {
          document.cookie= name + "=" + value +
              ((expires) ? "; expires=" + expires.toGMTString() : "") +
              ((path) ? "; path=" + path : "") +
              ((domain) ? "; domain=" + domain : "") +
              ((secure) ? "; secure" : "");
      }
    
      function getCookie(name) {
          var dc = document.cookie;
          var prefix = name + "=";
          var begin = dc.indexOf("; " + prefix);
          if (begin == -1) {
              begin = dc.indexOf(prefix);
              if (begin != 0) return null;
          }
          else {
              begin += 2;
          }
    
          var end = document.cookie.indexOf(";", begin);
          if (end == -1) {
              end = dc.length;
          }
    
          return unescape(dc.substring(begin + prefix.length, end));
      }
    
      function addItem(newItem, newPrice, newQuantity, newNumber, newWeight) {
        var cookieValue ;
        var expirationDate = new Date("December 31, 2040") ;
    
        if (newQuantity >= 1000) {
          alert('Wrong Quantity!');
        }
        else {
          if (newQuantity >= 1) {
            window.alert('' + newQuantity + ' x ' + newItem + ' Added to Cart!');
    
            cookieValue = getCookie("ShoppingBasket");
    
            if (cookieValue) {
              cookieValue += "[" + newItem + "|" + newPrice + "|" + newNumber + "|" + newWeight + "|" + newQuantity + "]";
            }
            else {
              cookieValue = "[" + newItem + "|" + newPrice + "|" + newNumber + "|" + newWeight + "|" + newQuantity + "]";
            }
    
            setCookie("ShoppingBasket", cookieValue, expirationDate)
          }
        }
    
        document.getElementById("CartContents").innerHTML = showBasket() ;
      }
    
      function showBasket() {
        var cartHTML ;
        vcartHTML = "" ;
    
        fulllist = getCookie("ShoppingBasket") ;
    
        subtotal = 0;
        cartHTML += '<CENTER><FORM NAME="updateform"><font size="5"><b><%=txt_cart%></B></FONT><HR WIDTH=75% size=1 COLOR="#000000">' ;
        cartHTML += '<table cellpadding="2" cellspacing="1" border="0">' ;
    
        cartHTML += '<TR><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_qty%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_code_no%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_item%></b></FONT></TD><TD BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_price%>&nbsp;<%=txt_currency%></b></FONT></TD><td BGCOLOR="#B3B3B3"><font size="-2"><b><%=txt_total%></b></FONT><TD BGCOLOR="#B3B3B3"><b><FONT COLOR="#B3B3B3">.</FONT></b></TD></TR>' ;
        itemlist = 0;
    
        for (var i = 0; i <= fulllist.length; i++) {
          if (fulllist.substring(i,i+1) == '[') {
            itemstart = i+1;
            thisitem = 1;
          }
          else if (fulllist.substring(i,i+1) == ']') {
            itemend = i;
            thequantity = fulllist.substring(itemstart, itemend);
            itemtotal = 0;
            itemtotal = (eval(theprice*thequantity));
            temptotal = itemtotal * 100;
            subtotal = subtotal + itemtotal;
            itemlist=itemlist+1;
            cartHTML += '<tr><td align=middle BGCOLOR="#D9D9D9"><INPUT TYPE=TEXT NAME="quant'+itemlist+'" VALUE="'+thequantity+'" SIZE=3></td><td BGCOLOR="#D9D9D9"><font size="-2"><a href=item_show2.asp?code_no='+thenumber+'>'+thenumber+'</A></FONT></td>' ;
    
            cartHTML += '<td align=left BGCOLOR="#D9D9D9"><font size="-2"><a href=item_show.asp?code_no='+thenumber+'>'+theitem+'</A></FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2">'+theprice+'</FONT></td><td align=right BGCOLOR="#D9D9D9"><font size="-2">'+ itemtotal +'</FONT></td><td WIDTH="38" align=right BGCOLOR="#D9D9D9"><a href="java_script_:updateItem('+itemlist+',document.updateform.quant'+itemlist+'.value)"><IMG SRC="images/update.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_update%>"></a><IMG SRC="images/space.gif" WIDTH="3" HEIGHT="2" ALT=""><a href="java_script_:removeItem('+itemlist+')"><IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_remove%>"></a>&nbsp;</td></tr>' ;
    
          }
          else if (fulllist.substring(i,i+1) == '|') {
            if (thisitem==1) theitem = fulllist.substring(itemstart, i);
            if (thisitem==2) theprice = fulllist.substring(itemstart, i);
            if (thisitem==3) thenumber = fulllist.substring(itemstart, i);
            if (thisitem==4) theweight = fulllist.substring(itemstart, i);
    
            thisitem++;
            itemstart=i+1;
          }
        }
    
      cartHTML += '<tr><td align=right b><%=txt_product_total%>&nbsp;<%=txt_currency%></b></FONT></td><td align=right font size="-2">'+ subtotal +'</FONT></td><td BGCOLOR="#B3B3B3"><FONT COLOR="#B3B3B3">.</FONT></td></tr>' ;
      cartHTML += '</TABLE><CENTER><font size="1"><IMG SRC="images/update.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_update%>"> = <%=txt_update%> <IMG SRC="images/remove.gif" WIDTH="13" HEIGHT="15" BORDER="0" ALT="<%=txt_remove%>"> = <%=txt_remove%></FONT></CENTER>' ;
      cartHTML += '</FORM>' ;
    
      return cartHTML ;
      }
      </script>
    </head>
    <body>
      <a href="#" onclick="addItem('123', 23.99, 45, '123Number', '123Weight');">Add 123 to Cart</a>
      <br>
      <a href="#" onclick="addItem('XYZ', 23.99, 45, 'XYZNumber', 'XYZWeight');">Add XYZ to Cart</a>
      <br>
      <a href="#" onclick="addItem('ABC', 23.99, 45, 'ABCNumber', 'ABCWeight');">Add ABC to Cart</a>
      <br>
      <a href="#" onclick="addItem('789', 23.99, 45, '789Number', '789Weight');">Add 789 to Cart</a>
      <br>
      <a href="#" onclick="alert(document.cookie);">Show Cookie</a>
      <div id="CartContents">
      <script>
        document.writeln(showBasket());
      </script>
      <div>
    </body>
    </html>
    Charles

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Manchester
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OMG Guimauve your an absolute diamond. I haev a little work to do to get the other scripts to work the same way but I won't bother you any more as I think I can do this..

    Many many thanks for you help!!


Bookmarks

Posting Permissions

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