/    Sign up×
Community /Pin to ProfileBookmark

Could someone help me with this please

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

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@KDLAAug 06.2009 — It might be better to do this with a database/server-side. If the viewer clears their history/cookies/cache, then this script will not work. Also, the database would be a better way of retaining those values.
Copy linkTweet thisAlerts:
@PasqualinoauthorAug 06.2009 — It might be better, but this is the last exercise I have to do for school and unfortunately I can't take the simplest way out, I have to do it this way.
Copy linkTweet thisAlerts:
@NedalsAug 06.2009 — You need to save the first name, family name, and color in the cookie (seperated by say ':') then use the split method to get them back when reading the cookie.
×

Success!

Help @Pasqualino 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.19,
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,
)...