Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to append to specific Cookie

Status
Not open for further replies.

johnboat

Technical User
Jun 18, 2008
4
Hi, I am a designer with HTML/CSS/some PHP skills but not much Javascript. I was hoping someone could give me some advice on this problem.

Goal: User clicks link ('More Info') that adds text string to a specific cookie. For example, cookie named 'products' has the value 'chair01' appended. Then, when the user is finished looking at products, I want to write out the cookie contents to a contact form.

Research: I have the basic idea working by modifying this tutorial CodePunk. My code at bottom of post.

Problem: I have another cookie my CMS (ExpressionEngine) uses and I don't won't to read/write to that. I am probably overlooking something simple. Also, is there an easy way to write out the cookie contents without the "cookie_name=" part?

Note: This is not an ecommerce system, so it is not important that the contents of this cookie are readable. It really is just an easy way for people to ask for information about a product without having to send multiple contact requests. I like the idea of using a simple cookie because it seems like overkill to use a database and the 4k cookie limit should be more than enough space for this simple application.

Thanks in advance for any help or suggestions.
Code:
[blue]Write cookie code:[/blue]
// in Head 
	<script language="JavaScript">
	function set_cookie(){
		if (document.cookie && document.cookie != ""){
		var old_cookie = document.cookie;
		var product = ("chair01<br/>");
		document.cookie = old_cookie + escape(product);
		}//ends IF

		else{
		var product = ("chair01");
		document.cookie = "products=<br/>" + escape(product);}
	}//ends function
	
	function kill_set_cookie(){
	var kill_date = new Date("January 1, 1970");
	var kill_string = "products=<br />;expires=" + kill_date;
	document.cookie = kill_string;
	}//ends function

	</script>
// end head

// in Body
<a href="javascript:set_cookie()">More info</a>
// end Body


[blue]Read cookie code:[/blue]
// in Head 
<script language="JavaScript">
	(document.cookie && document.cookie != "")
		{var whole_cookie = unescape(document.cookie);
	}
</script>
// end head

// in Body
<script language="JavaScript">document.write(whole_cookie);</script>
// end Body
 
You don't need to reinvent the wheel. A simple google search gave me this page that had this code. Use it to read/write your cookies without overwriting/deleting other cookies.
Code:
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);
}

Adam
 
Thanks Adam, I did find that code and some others (that's how I came across the CodePunk site). I had trouble getting it to do the appending (each time a user clicks a product it needs to add to the same cookie, not overwrite).

I am sure it is just because of my lack of JS knowledge. I will play with it some more.

Thanks again.
 
Thanks Adam, I am sure you are right. I will try again.
John
 
Adam, I finally got it working. It was my own stupidity (and tired brain) - I did not have the params in single quotes. Duoh!

Anyway, thanks for the help.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top