Hi Java Gurus,
I have to make a page with a form with 3 inputs, first name, family name and color(which will be set as the background color). The first time that they open the page the form needs to show but if they visited the page in the past I need to hide that form and display a message saying “Bonjour, “first name” + Family name + “bonne avant-midi” if it’s before noon, bonne après-midi if it’s before 6 and etc, and have the background color set to the one he chose on the first visit. Now I copied the script off a reference website for java and did alot or searching and modified the script for what I needed it to do but now I’m blocked.
I guess one of my biggest concern right now is how to save the three values in the cookie and retreive the same values from the cookie.
If someone could take a look at what I have so far and tell me if at least I’m going in the right direction, that would be greatly appreicated. And if someone can come up with the solution or the missing code that I need to make this work that would be even better hehe.
Here is what I have so far:
[CODE]<html>
<head>
<title>Exercice 7</title>
<script language=”javascript”>
function daytime()
{
var now = new Date()
var hour = now.getHours();
if (hour<12)
{
timemsg=”bonne avant-midi.”
}
else if (hour<18)
{
timemsg=”bonne après-midi.”
}
else (hour>=18)
{
timemsg=”bonsoir.”
}
return timemsg;
}
function setCookie(NameOfCookie, value, expiredays)
{
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + “=” + escape(value) + ((expiredays == null) ? “” : “; expires=” + ExpireDate.toGMTString());
}
function getCookie(NameOfCookie)
{
if (document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+”=”);
if (begin != -1)
{
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(“;”, begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end));
}
}
return null;
}
function DoTheCookieStuff()
{
username=getCookie(‘username’);
if (username!=null || username!=””)
{
document.getElementById(‘userinfo’).style.visibility = ‘hidden’;
document.write(“Bonjour, “+username+” “+famname+” “+timemsg)
}
else
{
document.getElementById(‘userinfo’).style.visibility = ‘visible’;
username = form.prenom.value;
famname = form.nomdefam.value;
backGcolor = form.choixdecouleur.value;
setCookie(‘username’,username,365)
}
}
// –>
</script>
</head>
<body>
<div id=”userinfo”>
<form name=”userinput”>
<h3>Entrez votre prénom:</h3><input type=”text” name=”prenom” size=20><br>
<h3>Entrez votre nom de famille:</h3><input type=”text” name=”nomdefam” size=20><br>
<h3>Veuillez choisir votre couleur préféré:</h3><select name=”choixcouleur”>
<option value=”black”>Noir</option>
<option value=”white”>Blanc</option>
<option value=”red”>Rouge</option>
<option value=”blue”>Bleu</option>
</select><br>
<input type=”submit” value=”Enregistrer” onclick=”DoTheCookieStuff()”>
<input type=”reset” value=”Effacer”>
</form>
</div>
</body>
</html>