/    Sign up×
Community /Pin to ProfileBookmark

Append data to a specific Cookie

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

[B]Goal:[/B] 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.

[B]Research:[/B] I have the basic idea working by modifying this tutorial <http://codepunk.hardwar.org.uk/ajs35.htm>. My code at bottom of post. I did find many other things by Googleing, but I had trouble getting all the bits from each to work.

[B]Problem:[/B] 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. I tried putting the name of the cookie in the parens, set_cookie(name), but I must be doing something wrong because I could not get that to work (but it works fine without it, except it writes over CMS cookie).

Also, is there an easy way to write out the cookie contents without the “cookie_name=” part?

[B]Note:[/B] 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.

[COLOR=”Blue”]Write cookie code:[/COLOR]

[CODE]// 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>[/CODE]

[COLOR=”Blue”]Read cookie code:[/COLOR]

[CODE]// 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>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 19.2008 — Look at this example where multiple cookies are read/write
Copy linkTweet thisAlerts:
@coolfritzauthorJun 19.2008 — Thanks Fang, I will take a look.
×

Success!

Help @coolfritz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...