www.webdeveloper.com
Results 1 to 5 of 5

Thread: Too many cookies

  1. #1
    Join Date
    Aug 2010
    Posts
    27

    Too many cookies

    I am using cookies to track form data the user inputs. The data only needs to be available during their session. I have well over 80 fields. How can I combine some of the cookies to cut down on the amount. I am storing them with JS and retrieving them with php.


    Any help is appreciated.

    Thanks

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Store as a concatenated list or as an array in 1 cookie

  3. #3
    Join Date
    Aug 2010
    Posts
    27
    Thank you for your answer. How would I store this? The cookie is build and rebuilt several times on the page. I would like to be able to build an associative array in the cookie but I am not clear how to do that. I would need to be able to check to cookie and determine whether say pm103 already has a value.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    pairs is the associative array
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    
    <script type="text/javascript">
    var pairs={};
    window.onload=function() {
    if(readCookie('wd')==null) {
        document.getElementById('data').innerHTML = 'no cookie';
        }
    else {
        var data = readCookie('wd');
        data=data.split(',');
        for(var i=0; i<data.length; i++){
            var tmp=data[i].split('=');
            pairs[tmp[0]]=tmp[1];
            }
        
        var f = document.myform;
        var updated = '';
        for (var i in pairs) {
            updated += i+" : "+pairs[i]+"<br>";
            }
        document.getElementById('data').innerHTML = updated;
        }
    };
    
    function saveValues() {
    var f = document.myform;
    pairs[f.elements['n'].value] = f.elements['v'].value;
    var data = [];
    for (i in pairs) {
        data.push(i + "=" + pairs[i]);
        }
    createCookie('wd', data, 1);
    location.reload(true);
    }
    
    // http://www.quirksmode.org/js/cookies.html
    function createCookie(name,value,days) {
    	if (days) {
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function eraseCookie(name) {
    	createCookie(name,"",-1);
    }
    </script>
    
    <style type="text/css">
    td {text-align:center;}
    p {border:1px solid; width:10em;}
    </style>
    
    </head>
    <body>
    <h3>cookie values</h3>
    <p id="data"></p>
    <form action="#" name="myform">
    <table>
    <thead>
    	<tr><th>name</th><th>value</th></tr>
    </thead>
    <tbody>
        <tr><td><input type="text" name="n"></td><td><input type="text" name="v"></td></tr>
    </tbody>
    <tfoot>
    	<tr><td colspan="2"><button type="button" onclick="saveValues();">update cookie</button></td></tr>
    </tfoot>
    </table>
    </form>
    </body>
    </html>

  5. #5
    Join Date
    Aug 2010
    Posts
    27
    Wow thank you very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles