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
10-27-2010, 12:42 AM
Fang
Store as a concatenated list or as an array in 1 cookie
10-27-2010, 07:54 AM
styks1987
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.
<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>