dipende da cosa devi farci,
questo copre (spero) tutto, prova a semplificarlo se non ti serve così completo
Codice PHP:
<html>
<head>
<title>salva form in cookie</title>
<script type="text/javascript" language="JavaScript">
<!--
var sep='<|>' // separatore
/***************************************\
funzioni generiche setta/leggi cookie
\***************************************/
function setCk(nome, valore, giorni) {
var date = new Date();
date.setTime(date.getTime()+(giorni*24*60*60*1000));
document.cookie = nome+'='+valore+'; expires='+date.toGMTString()+'; path=/';
}
function getCk(nome) {
var nameEQ = nome + '=';
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;
}
/***************************************\
funzioni salva/setta stato campi form
\***************************************/
function salva(){
var f=document.modulo
var str=''
for(var k=0;k<f.elements.length;k++){
var c=f.elements[k]
if(c.type=='text' || c.type=='textarea' || c.type=='password') str+=c.value
else if(c.type=='checkbox' || c.type=='radio') str+=((c.checked)?1:0)
else if(c.type=='select-one') str+=c.selectedIndex
else if(c.type=='select-multiple') {
for(var q=0;q<c.options.length;q++){
str+=((c.options[q].selected)?1:0)+((q!=c.options.length-1)?'|':'')
}
}
else str+=sep // "file", "button", "submit" e "reset" da non settare
if(k!=f.elements.length-1)str+=sep
}
setCk('str',str,365)
}
function setta(){
if(getCk('str')){
var f=document.modulo
var str=getCk('str').split(sep)
for(var k=0;k<f.elements.length;k++){
var c=f.elements[k]
if(c.type=='text' || c.type=='textarea' || c.type=='password') c.value=str[k]
else if(c.type=='checkbox' || c.type=='radio') c.checked=parseInt(str[k])
else if(c.type=='select-one') c.selectedIndex=str[k]
else if(c.type=='select-multiple') {
var p=str[k].split('|')
for(var q=0;q<c.options.length;q++){
c.options[q].selected=parseInt(p[q])
}
}
}
}
}
window.onload=setta
//-->
</script>
</head>
<body>
<form name="modulo" onsubmit="if(this.salva_in_cookie.checked){salva()}">
<input type="text">
<input type="password">
<textarea></textarea>
<input type="checkbox">
<input type="radio" name="a">
<input type="radio" name="a">
<select>
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<select multiple="multiple">
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<input type="file">
<hr />
<input type="checkbox" name="salva_in_cookie"> salva in cookie
<hr />
<input type="submit">
</form>
</body>
</html>
ciao