Page 1 of 1

can not store values in cookies

Posted: Thu Oct 25, 2007 1:28 pm
by goutam.dutta123
Hello experts,

I want to set the values of my checkboxes into cookies when checked.Here is my code:

***** PLEASE USE TAGS WHEN POSTING CODE *****

Code: Select all

function keepID(value)
   {
  i = 0;
  var ca = document.cookie.split(';');
  for(i=0;i<<?php echo $_SESSION['row'] ?>;i++)
     {
  if(document.myform.chkID[i].checked==true)
   {  
     ca[i]=value;
	 
   }
      }
	    
    }
My checkboxes are moving in a loop:

Code: Select all

<input type="checkbox" name="chkID[]" id="chkID" value="<?=$rowSubjects['ID']?>" onClick="keepID(<?=$rowSubjects['ID']?>)">
I can not set these ids .
Please help..

Posted: Thu Oct 25, 2007 3:35 pm
by feyd
I doubt this is PHP's problem. Moved to Client-side.

Posted: Thu Oct 25, 2007 4:46 pm
by VladSun
Standard way:

Code: Select all

function getCookie(name) 
{
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return(null);
    if (start == -1) return(null);
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return(unescape(document.cookie.substring(len,end)));
}

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" : "");
}

can not set check box's values into cookies

Posted: Fri Oct 26, 2007 12:18 am
by goutam.dutta123
Hello experts,

Thanks for the reply.

In my php pagination ,i want to set the check box's values(Book ids coming from database) when they are checked.So that when i come respective pages by clicking the respective links in my pagination ,the check boxes which were checked before remain checked.
I have seen your reply but in my case how i use it so that at a time more than one check box's values can be set into cookies as well as get from the cookies.

Help please....

Posted: Fri Oct 26, 2007 7:19 am
by VladSun
Maybe using $_SESSION is more appropriate in this case ... or you could serialize the $_POST checkbox array and pass it to the next page via hidden field. I don't think using cookies in this case is a good solution.