can not store values in cookies

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
goutam.dutta123
Forum Newbie
Posts: 2
Joined: Thu Oct 25, 2007 1:21 pm

can not store values in cookies

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I doubt this is PHP's problem. Moved to Client-side.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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" : "");
}
There are 10 types of people in this world, those who understand binary and those who don't
goutam.dutta123
Forum Newbie
Posts: 2
Joined: Thu Oct 25, 2007 1:21 pm

can not set check box's values into cookies

Post 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....
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply