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 7 of 7
  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts

    cookies with lots of info

    I posted earlier to a different area of the forum that I wish to save ONE byte of information (a number between the value of 0 and 9) from 1000 different pages (yes, that's right and I'm not mad - the pages are already written) per user.

    Cookies leaps to mind.

    Yet felgall replied (and I quote) "A web site can only use a maximum of 20 cookies containing not more than 4096 bytes of data (in each for browsers other than IE and between them for IE).

    The best solution when you need to store huge amounts of data like that is to store it in a database and just use a single cookie to identify the computer that is accessing the pages so as to access their specific database entry."

    I never doubt felgall - thanks.

    But I think my situation can be resolved with the use of cookies because:

    a) the situation is offline (I don't need to record more than one user)
    b) I only need to record a maximum of 1000 x 1 bytes

    Of course, I could (easily) be wrong.

    I am scared because of what felgall wrote: "A web site can only use a maximum of 20 cookies:.

    But I think I am only recording the information in one cookie. I got this code from the source below (the best cookie example online in my opinion):

    Code:
    // -----------------------------------------------------//
    // SAVE FORM FIELD SELECTIONS IN COOKIES 		//
    // formcookie_saverestore.js				//
    // Written by Tony Davis, T. Davis Consulting, Inc.	//
    // Date written: September 27, 2005			//
    // Email: tony@tdavisconsulting.com			//
    // Web site: http://www.tdavisconsulting.com		//
    // -----------------------------------------------------//
    
    // posted here:
    // http://javascript.internet.com/cookies/save-and-restore-form-cookies.html
    
    // instructions:
    // ------------
    // Change <BODY>
    // to <BODY onunload="saveSelections(document.forms[0])"> and 
    // Change </FORM> 
    // TO </FORM><SCRIPT language=JavaScript type="">loadSelections(document.forms[0]);</SCRIPT>
    // see a working example at: http://www.tdavisconsulting.com/formcookie
    // 
    
    //
    // ------------------------------------------------
    // This function will concatentate all the fields in	
    // in the form into one string, delimited by a PIPE
    // symbol, into one cookie. The cookie name is the 
    // same name as the form name. ALL fields are saved.
    // ------------------------------------------------
    //  
    
    
    function saveSelections(frm) {
    		
    		var setvalue;
    		var fieldType;
    		var index;
    		
    		var formname = frm.name;
    						
    		// Expire cookie in 999 days.
    		var today = new Date();
    		var exp   = new Date(today.getTime()+999*24*60*60*1000); 
    		
    		var string = "formname=" + formname + "|";
    		var cookieName = formname;
    
    		//alert(exp);
    		//alert(formname);
    		
    		var n = frm.length;
    		for (i = 0; i < n; i++)
    		
    		{
    			e 	    = frm[i].name;
    			fieldValue  = frm[i].value;	
    			fieldType   = frm[i].type;
    			
    			//alert(e);
    			//alert(fieldType);
    			//alert(fieldValue);
    						
    			//
    			// RADIO BUTTON
    			//
    			if (fieldType == "radio") {
    			//alert(frm.elements[e].length);
    				for (x=0; x < frm.elements[e].length; x++) {
    					if (frm.elements[e][x].checked)	
    					{
    					index = x
    					}
    				}
    			string = string + index + "\|";
    			}
    			
    			
    			//
    			// TEXT, TEXTAREA, and DROPDOWN
    			//
    			if ((fieldType == "text") ||
    			    (fieldType == "textarea") ||
    			    (fieldType == "select-one")) 
    			{
    		    	string = string + frm.elements[e].value + "\|";
    		    	//alert("text");
    			}
    			
    			
    			
    			//
    			// CHECKBOX
    			//
    			if (fieldType == "checkbox") 
    			{
    				if (frm.elements[e].checked==true) {
    					var setvalue = "1";
    					}
    				if (frm.elements[e].checked==false) {
    					var setvalue = "0";
    					}
    			string = string + setvalue + "\|";
    			//alert("checkbox");
    			}
    			
    			
    			//
    			// HIDDEN field
    			//
    			if (fieldType == "hidden") 
    			{
    		    	string = string + frm.elements[e].value + "\|";
    		    	//alert("text");
    			}
    
    			
    		}
    
    //alert(string);
    setCookie(cookieName, string, exp);
    
    }
    
    
    
    //
    // LOAD FORM FIELD SELECTIONS FROM SAVED COOKIES
    // 
    
    function loadSelections(frm) {
    
    var e;
    var z;
    var x;
    var cookieName;
    var fieldArray;
    var fieldValues;
    var fieldValue;
    
    var formname = frm.id;
    
    // Retrieve form elements from cookie and split into array.
    
    cookieName  = formname;
    fieldValues = getCookie(cookieName);
    fieldArray  = fieldValues.split("\|");
    
    //alert(fieldArray);
    //alert(fieldArray[0]);
    //alert(fieldArray[1]);
    //alert(fieldArray[2]);
    //alert(fieldArray[3]);
    
    		var n = frm.length;
    		for (i = 0; i < n; i++)
    		
    			{
    			
    			e = frm[i].name;
    			z = i;
    			
    			z++;
    			
    			var fieldType  = frm[i].type;
    			var fieldValue = fieldArray[z];
    			
    			//
    			// TEXT, TEXTAREA, and DROPDOWN
    			//
    			if ((fieldType == "text") ||
    			    (fieldType == "textarea") ||
    			    (fieldType == "select-one")) 
    			{
    		    	frm.elements[e].value = fieldValue;
    		    	//alert(e);
    		    	//alert(fieldValue);
    			}
    	
    			//
    			// CHECKBOX
    			//
    			if (fieldType == "checkbox") 
    			{
    				fld_checkbox = fieldValue;
    				if (fld_checkbox == "1") {
    					frm.elements[e].checked = true;
    				}
    			}
    			
    			
    			//
    			// RADIO BUTTON
    			//
    			if (fieldType == "radio") {
    				x = fieldValue;
    				//alert(x);
    				frm.elements[e][x].checked = true;
    			}
    		
    			
    			//
    			// HIDDEN field
    			//
    			if (fieldType == "hidden")
    			{
    		    	frm.elements[e].value = fieldValue;
    		    	}		
    		
    
    		}
    
    }
    		
    
    ///
    /// COOKIE FUNCTIONS
    ///
    
    function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(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));
    }
    Does anyone know if I am barking up the wrong tree?

    So far I have tested it with 10 pages. Should I continue with the other 990?

    Thanks,

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Is each page using the same cookie?
    If so, sounds OK to me.

    -tdavis

  • #3
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Yes, each page is using the same cookie.

    Thanks - I feel a little more confident now! Just didn't want the thing to explode further on down the track...

  • #4
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    I now have a problem trying to read back from the cookie on a page in a different folder.

    I have one page (page1) which accepts and records a number(r1):

    Code:
    <body onload="loadSelections(document.getElementById('r1'))">
    <form method="get" id="r1" name="r1">
    RATING:
    <input type="text" name="r1" size="1" maxlength="1">
    </form>
    <a onclick="saveSelections(document.getElementById('r1'))" title="Save form values" href="#">
    <img src="change.gif" alt="Save form values" name="mySubmit"></a>
    It calls the same external .js file as the second page, but the second page only works if it is in the same folder as page1.

    I assume it has something to do with:

    Code:
    <body onload="loadSelections(document.getElementById('r1'))">
    What should I be using to read back the information (r1) from a second page in different folder?

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    If your HTML pages are in different folders then I think you would adjust this line to reflect where the script is. The following assumes the script is in the same directory as the page. I am not sure if the second example works. That is the method used in Perl to back up on directory.

    <script type="text/javascript" src="formcookie_saverestore.js" name="formCookieRestore"></script>

    <script type="text/javascript" src="../formcookie_saverestore.js" name="formCookieRestore"></script>

  • #6
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    884
    Thanks
    53
    Thanked 6 Times in 5 Posts
    Hi tdavis,

    As you can see I am using your script to try to do what I want. It's a good script for pulling up information on the same page.

    I'm not sure the js file reference is the problem...

    My cookie skills are practically non-existent, but I am trying to read cookies on different pages and in different folders.

    I have set up a demo:

    http://aapress.com.au/demo/setratings/page1.html

    Having set a rating on page 1, you go to page 2 and do the same - setting a different rating.

    Then on to page three where I want to be able to read both the page 1 and page 2 ratings.

    Trouble is, it doesn't work at all.

    Only one rating is being set and page 3 doesn't do anything.

    I get "null" errors as well. I don't see why - I tried setting an initial value for both input boxes, but it didn't solve it.

    Does anyone have any ideas?
    Last edited by tpeck; 10-20-2006 at 04:13 PM.

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Mabye if you add a hidden field for field "r2" on page 1 and for "r1" on page 2, that way they do not overlay each other, and both will be accounted for (in the cookie) on page 3.


  •  

    Posting Permissions

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