Page 1 of 1

Cookie with multiple values

Posted: Tue Nov 21, 2006 5:11 am
by jtron85
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to set multiple values into a cookie and I'm having trouble figuring out how to create a function for it. I'm setting this up for a customized search result in php where users can select various items with checkboxes.

A cookie is then create whilst the user clicks the checkbox, at the moment I'm using this to create the cookie:

[syntax="javascript"]

function getCookie( name ) {//get cookie name
  var start = document.cookie.indexOf( name + "=" ); //variable start
  var len = start + name.length + 1; //variable len
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;//if vars do not exists
  }
  if ( start == -1 ) return null; //if var is negative
  var end = document.cookie.indexOf( ";", len ); //end variable
  if ( end == -1 ) end = document.cookie.length; //if end is negative
  return unescape( document.cookie.substring( len, end ) ); //return cookie
}

function setCookie(name, value)
	{
  	var today = new Date();
        var expires_date = new Date( today.getTime() + (24*60*60*1000*60) ); // cookie persist for 60 day
	document.cookie = name+"="+escape(value)+";path=/;expires=" + expires_date.toGMTString();
	}

I haven't created the function which joins the checkbox to the cookie just as yet.
I was wondering if anyone has done anything like this or has any idea on how I could put this together. Any help would be much appreciated.

Also I'm planning to display the selected list with the use of PHP so how hard would it be to display cookies created with JS with PHP?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Nov 21, 2006 7:18 am
by feyd
Why do you want to store checkboxes into a cookie when it's far more simple to simply submit the form?

Posted: Tue Nov 21, 2006 8:05 am
by jtron85
Well basically each search result page displays 10 items. The number of pages can vary depending on the amount of items that fit the search criteria. The user will be allowed to choose any number of items from the search result spanning across all the pages and any other new searches - this is where the cookies come in.

If I was to simply submit the form would I have to set every link with a submit function which would include "next, back, new search etc" just to keep record of what the user has chosen?

Posted: Tue Nov 21, 2006 8:11 am
by feyd
jtron85 wrote:If I was to simply submit the form would I have to set every link with a submit function which would include "next, back, new search etc" just to keep record of what the user has chosen?
That's the general idea. What happens if someone has Javascript off, or not supported? If they can't browse, that's often a horrible user experience waiting to happen. You can always use unobtrusive Javascript to replace a submit button with textual or graphical links.

Store the submission data into a session variable.

Posted: Tue Nov 21, 2006 8:34 am
by jtron85
Oh ok thanks again feyd appreciate it.

It's back to the old drawing board for me as usual.