/    Sign up×
Community /Pin to ProfileBookmark

Need help making cookies

There are a lot of extremely smart members here, so I thought if the knowledge is out there, this would be the place to tap into it. I have included the code below that I am working with. What I want is for a person to enter their name and room number and coffee preference. Then a cookie will store that information and tell them a cup will be sent to them at their room. When the page is loaded again their preference will be remembered and they will be offered a discount for their favorite coffee. This is part of a self taught book I am studying.

Most of it works, but I think the selection of coffee from the radio button is not being stored in the cookie, or at least I can not recall it. I was also wondering if there was some way that I could combine the makeCookie and welcome functions so that there would only be one button to click on the page.

Any ideas???

[code]
<html>

<head>
<title>Making a Cookie</title>
<script language=”JavaScript”>
function makeCookie(form){
var when = new Date();
when.setTime(when.getTime() + 24 * 60 * 60 * 1000);
// 24 hours from now
when.setFullYear(when.getFullYear() + 1);
// One year from now

yname=document.form1.yourname.value;
ycup=document.form1.coffee.value;
yroom=document.form1.yourroom.value;
document.cookie=escape(“name”)+”=”+escape(yname, ycup, yroom)+
“;expires=”+when.toGMTString();

alert(document.cookie);
}
function welcome(){
you=document.form1.yourname.value;
room=document.form1.yourroom.value;
cup=document.form1.coffee.value;
var position=document.cookie.indexOf(“name=”);
if ( position != -1){
var begin = position + 5;
var end=document.cookie.indexOf(“;”, begin);
if(end == -1){ end=document.cookie.length;}
you= unescape(document.cookie.substring(begin, end));
alert(“Welcome ” + you + “,nnwe will be sending a fresh cup of ” + cup + “nnto you in room number ” + room );
}
else{ alert(“No cookies today”);}
}

function seeDiscount(){
you=document.form1.yourname.value;
room=document.form1.yourroom.value;
cup=document.form1.coffee.value;
var position=document.cookie.indexOf(“name=”);

if(document.cookie == “”){
alert(“No cookies totay”);
return false;
}
else {
if ( position != -1){
var begin = position + 5;
var end=document.cookie.indexOf(“;”, begin);

if(end == -1){ end=document.cookie.length;}
you= unescape(document.cookie.substring(begin, end));
alert(“Welcome back ” + you + “,nnGet a fresh cup of ” + cup + ” at 25% off today.” );
}
else{ alert(“No cookies today”);}
}}

</script>
</head>

<body onLoad=”seeDiscount()”>

<h2>Room Service</h2>
<form name=”form1″>
What is your name? <br>
<input type=”text” name=”yourname” size=”30″>
<p>What is your room number? <br>
<input type=”text” name=”yourroom” size=”3″> </p>
<p>What kind of coffee would you like?</p>
<p><input type=”radio” name=”coffee” value=”Espresso”>Espresso<br>
<input type=”radio” name=”coffee” value=”Cappucino”>Cappucino<br>
<input type=”radio” name=”coffee” value=”Mocha”>Mocha<br>
</p>
<p>
<input type=”button” value=”Make cookie” onclick=”makeCookie();”>
</p>
<p><input type=”button” value=”Welcome (Get cookie)” onclick=”welcome();”>
</p>
<p></p>
</form>

</body>

</html>
[/code]

CarolinaN

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 10.2005 — The following thread demonstrates a reliable method for creating and retrieving cookie values -- and includes some standard cookie functions that make the whole process much easier.

http://www.webdeveloper.com/forum/showthread.php?t=68589
Copy linkTweet thisAlerts:
@Mr_JJun 10.2005 — Please give the following a try


[code=php]
<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script type="text/javascript">
<!--
cookie_name="coffee_cup"
expdays=1

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

deleting=0 // allows cookie to be deleted while testing, delete this line when done

function delete_cookie(name,path,domain){

deleting=1 // allows cookie to be deleted while testing, delete this line when done

document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

Data=""
function save_data_to_cookie(){ // compile data for cookie

if(deleting==1){return} // allows cookie to be deleted while testing, delete this line when done

var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000)); // expiry date

this.form=document.form1
for(el_num=0;el_num<this.form.length;el_num++){ // run through form elements
this.type=this.form.elements[el_num].type
this.name=this.form.elements[el_num].name
this.value=this.form.elements[el_num].value

if (this.type=="text"){
Data+=this.name+"="+this.value+"&" // add element name and value
}

if(this.type=="radio"){
if(this.form.elements[el_num].checked){
Data+=this.name+"="+this.value+"&" // add element name and value
}
}

}

set_cookie(cookie_name,Data,expdate)
}

function get_cookie_data(){ // decompile data from cookie
inf=get_cookie(cookie_name)
if(!inf){return}

this.form=document.form1
form_info=inf.split("&") // split the string at character & and create an array of values

user_info=new Array() // welcome information

for(el_num=0;el_num<this.form.length;el_num++){ // run through form elements

this.type=this.form.elements[el_num].type
this.name=this.form.elements[el_num].name
this.value=this.form.elements[el_num].value

for(nv=0;nv<form_info.length-1;nv++){
name_value=form_info[nv].split("=") // get value at form_info index nv and split at character =

user_info[nv]=name_value[1] // add to array

if(this.type == "text"&&this.name==name_value[0]){
this.form.elements[el_num].value=name_value[1] // and assign value
}

if(this.type == "radio"){
if(this.value==name_value[1]){ // check against value
this.form.elements[el_num].checked=true // check radio
}
}

}

}

alert("Welcome " +user_info[0] + ",nnwe will be sending a fresh cup of " +user_info[2]+ "nnto you in room number " + user_info[1] );


}

// add onload="get_cookie_data()" onunload="save_data_to_cookie()" to the opening BODY tag

// -->
</script>


</HEAD>
<BODY onload="get_cookie_data()">
<h2>Room Service</h2>

<form name="form1">
What is your name? <br>
<input type="text" name="yourname" size="30">

<p>What is your room number? <br>
<input type="text" name="yourroom" size="3"> </p>

<p>What kind of coffee would you like?</p>

<p><input type="radio" name="coffee" value="Espresso">Espresso<br>
<input type="radio" name="coffee" value="Cappucino">Cappucino<br>
<input type="radio" name="coffee" value="Mocha">Mocha<br>
</p>

<p>
<input type="button" value="Make cookie" onclick="save_data_to_cookie()"">
</p>
<p><input type="button" value="Delete cookie" onclick="delete_cookie(cookie_name)" title="For testing purposes only, delete this button when done">
</p>
<p></p>
</form>


</BODY>
</HTML>


[/code]
×

Success!

Help @CarolinaN 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...