JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 10th, 2005, 08:11 AM
CarolinaN CarolinaN is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 1 CarolinaN User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 6 sec
Reputation Power: 0
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 + ",\n\nwe will be sending a fresh cup of " + cup + "\n\nto 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 + ",\n\nGet 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>

CarolinaN

Reply With Quote
  #2  
Old June 30th, 2006, 10:46 PM
colton22's Avatar
colton22 colton22 is offline
\ ^_^ / - Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: near chicago, Illinois
Posts: 473 colton22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 19 m 14 sec
Reputation Power: 4
Send a message via AIM to colton22 Send a message via MSN to colton22 Send a message via Yahoo to colton22
ok i got a solution...

i dont know why the other didnt work, but here it is...




<html>

<head>
<title>Making a Cookie</title>
<script language="JavaScript">
var cup="";
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=cup;
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;
//alert(cup);
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 + ",\n\nwe will be sending a fresh cup of " + cup + "\n\nto 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 today");
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 + ",\n\nGet a fresh cup of " + cup + " at 25% off today." );
}
else{ alert("No cookies today"); }
}}
function updateCup(val) {cup=val;}

</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" onClick="updateCup('Espresso')">Espresso<br>
<input type="radio" name="coffee" onClick="updateCup('Cappucino')">Cappucino<br>
<input type="radio" name="coffee" onClick="updateCup('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>


hope this helped

colton22

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Need help making cookies


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT