SEO | Link Popularity | Search Engine Consulting | SEO Tutorial | SEO Tools | SEO Forum
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-10-2005, 06:41 AM
CarolinaN CarolinaN is offline
SEO Junior
 
Join Date: Jun 2005
Posts: 2 CarolinaN is on a distinguished road
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
Reply


Thread Tools
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
Forum Jump


Login/Register
User Name
Password
Remember Me?

Forum Links
Forum Home
SEO Forum
Internet Marketing Forum
Web Design Forum
Web Hosting Forum
Programming Forum
SEO Chat

Quick Links
Forum Home
New Posts
Mark Forums Read
Open Buddy List
User Control Panel
Edit Avatar
Edit Profile
Edit Options
Miscellaneous
Subscribed Threads
My Profile

Search Forums

Advanced Search
All times are GMT -8. The time now is 02:19 AM.


Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.