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 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Retrieving cookie values

    I had post this problem before, dealing with the shopping cart application. The application so far does everything that i would like it to do. I'm able to store and retrieve the cookies. But right now, my problem is extracting the individual values from the cookies and placing them into a table i have created. This is the format of the cookie when retrieved.
    ItemName = quantity$price. what i am trying to do is retrieve the quantity and price values from the cookie, and display that data into my table. the cookie is being created in my store page, and then retrieved and displayed in my cart page. here is the code for my store page

    store.html
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
    transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" 
    lang="en">
    
    <head>
    <title> Store </title>
    <h1> My store </h1>
    <script type="text/javascript">
    
    
    
    function retr(circle)
    {
    var cke = document.cookie.split(';');
    	var nameCk= name + "=";
    	for(var i=0; i < cke.length; i++)
    	{
       		var c = cke [i];
    		while (c.charAt(0)==' ') c = c.substring(1, 
    
    c.length);
    		if (c.indexOf(nameCk) == 0) return 
    
    c.substring(nameCk.length, c.length);
       	 }
      return null;
    }
    function createCookie() 
    {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() +10);
     if (document.getElementById("circle").checked)
    {
    document.cookie = "Circle=" + document.getElementById
    
    ("quantity1").value + document.getElementById("price1").value + 
    
    ";expires="+exdate.toGMTString();
    }
    }
    
    
    
    function retrieve() 
    {
     if (document.getElementById("circle").checked)
    {
    document.getElementById("ret").value = document.cookie;
    }
    }
    
    function Calc()
    {
         var fpri = document.getElementById("price1").value;
         var pri = fpri.substr(1);
         var qty = document.getElementById("quantity1").value;
         var spri = document.getElementById("price2").value;
         var pri2 = spri.substr(1);
         var qty2 = document.getElementById("quantity2").value;
        
     if (document.getElementById("circle").checked)
    {
         document.getElementById("total").value ='$' + Number(pri) * 
    Number(qty);
    }
    else
         document.getElementById("total").value = '$' + Number(pri2) * 
    Number(qty2);
    }
    </script>
    
    </head>
    
    <body>
    <table border = "1">
    <tr>
      <td> <input type="checkbox" id="circle" /> Circle </td>
      <td> <img src="circle.jpg"> </img> </td>
      <td> Price: <input type="text" size="4"   value = "$10.00" 
    id="price1" name = "price1" /></td>
      <td> Quantity: <input type="text" size = "4"  
    id="quantity1"/></td>
    </tr>
    <tr>
      <td> <input type = "checkbox" id="stickman" /> Stickman </td>
      <td> <img src = "stickman.gif"> </img> </td>
      <td> Price: <input type="text" size="4" value = "$5.00" 
    id="price2" /> </td>
      <td> Quantity: <input type="text" size="4"  id="quantity2" /> 
    </td>
    </tr>
    
    </table>
    <br />
    <input type = "button" value = "Add to cart" 
    onclick="createCookie()">
    <br />
    <br />
    <a href ="cart.html" > View Cart </a>
    <br /> 
    <input type = "text" size = "8" id = "total" readonly = "readonly" 
    /> Total
    <br /> 
    <input type = "button" id = "calcu" value = "calc" onclick = "Calc
    ()" />
    <input type = "button" value = "retrieve" onclick = "retrieve()" />
    <input type = "text" readonly = "readonly"  name = "ret" id = 
    "ret"/>
      
    
    
    
    
    </body>
    </html>
    the calculate and retrieve buttons, and the readonly text boxes are there just so i know the cookie is being stored and retrieved, and that i'm able to get the total, which will be displayed on my cart page.

    here is the code for my cart page
    cart.html
    Code:
    <html>
    <head>
    <title> Cart </title>
    <h1> My cart </h1>
    <script type = "text/javascript">
    
    function retr(circle)
    {
    document.getElementById("q2").value = document.getElementById("quantity1").value
    
    }
    
    function retrieve() 
    {
    var quvalue = retr("circle")
    if (quvalue != false) {
    document.getElementById("q2").value = quvalue
    }
    document.getElementById("ret").value = document.cookie;
    
    }
    
    
    </script>
    </head>
    
    <body onload = "retrieve()">
     <table border = "1">
     <td>Stickman </td>
     <td><input type = "text" size = "8" id = "q2" readonly = 
    
    "readonly" /></td> 
     <td id ="p1"> price per </td>
     <td id ="t1"> total </td>
     <tr> </tr>
     <td> Circle </td>
     <td> quantity order </td>
     <td> price per </td>
     <td> total </td>
     <tr> </tr>
     <td colspan = "3"> TOTAL: </td>
     <td> total price </td>
    </table> 
    <br /> <br />
    <input type ="text" id = "ret" readonly = "readonly" />
    <br / > <br />
    <input type = "button" value = "Checkout">
    <br /> <br />
    <a href = "store.html" > Continue Shopping </a>
    
    
    </body>
    </html>
    the values that are supposed to be retrieved in the table are invoked using the onload event attribute which is suppose to call the retrieve() function.
    Last edited by needhelp101; 03-23-2012 at 12:47 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts
    If this isn't a class project, try NOPCart. And if it is, study NOPCart and how they work with cookies.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.


  •  

    Posting Permissions

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