/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Using Cookie Values

I am working on a form that will save cookie information for visitors. I want it to work like this:
When a user submits the registration form, it should store cookies containing the user’s information such as name, company, and so on. If a user attempts to register a second time with the same name, display a confirm dialog box asking if he or she wants to register again.

I can’t get this to work at all. This is my first time working with cookies to actually save data so any help would be appreciated. What am I doing wrong?
Here’s my code so far:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml“>

<head>
<title>Marketing Seminar</title>
<script type=”text/javascript”>

function registerForm()
{
document.cookie = “firname=” + encodeURIComponent(document.form1.fname.value);
document.cookie = “lasname = last_name” + encodeURIComponent(document.form1.lname.value);
var reregister = true
if (document.cookie = null)
{
var firstName = “”
var lastName = “”
firstName = decodeURIComponent(document.cookie)
window.alert(‘Would you like to re-register?’)
else
reregister = false
window.alert(‘Thank you for registering!’)
}
</script>

</head>
<body>
<h1>Marketing Seminar Registration</h1>
</br>
<form action=”” name=”form1″>
First Name: <input type=”text” size=”15″ name=”fname”>
Last Name: <input type=”text” size=”15″ name=”lname”>
</br>
Street Address1: <input type=”text” size=”50″ name=”saddress1″>
</br>
Street Address 2: <input type=”text” size=”50″ name=”saddress2″>
</br>
City: <input type=”text” size=”30″ name=”city”>
State: <input type=”text” size=”2″ name=”state”>
Zip: <input type=”text” size=”5″ name=”zip”>
</br>
Phone: <input type=”text” size=”15″ name=”phone”>
Fax: <input type=”text” size=”15″ name=”fax”>
</br>
Name you would like on your badge: <input type=”text” size=”30″ name=”bname”>
</br></br>
<input type=”button” name=”submit” value=”Submit” onclick=”registerForm();”>
</form>

<script type=”text/javascript”>

</script>

</body>
</html>

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 20.2010 — It's important that you understand how to read and write cookies correctly.
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 20.2010 — I do understand what cookies are and how they work, I am just not quite sure how to use them with this assignment. My professor is looking for me to create a cookie in a function called registerForm() and his directions say nothing about creating a cookie using the CreateCookie() function. THAT'S what I'm confused about. I don't know how to integrate the form code with the cookie and can't seem to find a code example for it. I am a very visually oriented learner and really need to see a code similar to my needs before I can understand it.
Copy linkTweet thisAlerts:
@FangNov 20.2010 — This explains it in basic terms: http://www.w3schools.com/JS/js_cookies.asp
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 21.2010 — Okay so this is what I have now but It's only asking if I would like to re-register. What did I do?

Please just look it over and give me code I'm missing or need to fix.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Marketing Seminar</title>

<script type="text/javascript">

function getCookie(c_name)

{

if (document.cookie.length>0)

{

c_start=document.cookie.indexOf(c_name + "=");

if (c_start!=-1)

{

c_start=c_start + c_name.length+1;

c_end=document.cookie.indexOf(";",c_start);

if (c_end==-1) c_end=document.cookie.length;

return unescape(document.cookie.substring(c_start,c_end));

}

}

return "";

}

function setCookie(c_name,value,expiredays)

{

var exdate=new Date();

exdate.setDate(exdate.getDate()+expiredays);

document.cookie=c_name+ "=" +escape(value)+

((expiredays==null) ? "" : ";expires="+exdate.toUTCString());

}

function registerForm()

{

username=getCookie('username');

if (username!=null && username!="")

{

window.alert('Would you like to re-register?');

}

else

{

window.alert('Thank you for registering!');

username= document.form1.fname.value

lastname= document.form1.lname.value

stree1= document.form1.saddress1.value

stree2= document.form1.saddress2.value

ucity= document.form1.city.value

ustate= document.form1.state.value

uzip= document.form1.zip.value

uphone= document.form1.phone.value

ufax= document.form1.fax.value

if (username!=null && username!="");

setCookie('username',username,365);

setCookie('lastname',lastname,365);

setCookie('stree1',stree1,365);

setCookie('stree2',stree2,365);

setCookie('ucity',ucity,365);

setCookie('ustate',ustate,365);

setCookie('uzip',uzip,365);

setCookie('uphone',uphone,365);

setCookie('ufax',ufax,365);

}

}

}

</script>

</head>

<body>

<h1>Marketing Seminar Registration</h1>

</br>

<form action="" name="form1">

First Name: <input type="text" size="15" name="fname">

Last Name: <input type="text" size="15" name="lname">

</br>

Street Address1: <input type="text" size="50" name="saddress1">

</br>

Street Address 2: <input type="text" size="50" name="saddress2">

</br>

City: <input type="text" size="30" name="city">

State: <input type="text" size="2" name="state">

Zip: <input type="text" size="5" name="zip">

</br>

Phone: <input type="text" size="15" name="phone">

Fax: <input type="text" size="15" name="fax">

</br>

Name you would like on your badge: <input type="text" size="30" name="bname">

</br></br>

<input type="button" name="submit" value="Submit" onclick="registerForm();">

</form>

<script type="text/javascript">

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@Logic_AliNov 21.2010 — Okay so this is what I have now but It's only asking if I would like to re-register. What did I do?[/quote]You use the error console.

Your [I]registerform[/I] function has one closing brace too many and therefore isn't executing.

In this particular case the console can't pinpoint the exact problem but at least you'd have had a clue.

When you post code, kindly enclose it within tags so that it keeps the indented formatting that (I hope) you gave it. In the editor, these tags are insertable around a highlighted block by clicking the # symbol.
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 21.2010 — What is the "error console"? I'm sorry if I am frustrating people but I am very new at this and taking a class where I am getting zero instruction with no help.

[CODE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Marketing Seminar</title>
<script type="text/javascript">

function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=encodeURI(document.cookie.length);
{
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function registerForm()
{
username=decodeURI(getCookie('username'));
if (username!=null && username!="")
{
window.alert('Would you like to re-register?');
}
else
{
window.alert('Thank you for registering!');
username= document.form1.fname.value
lastname= document.form1.lname.value
stree1= document.form1.saddress1.value
stree2= document.form1.saddress2.value
ucity= document.form1.city.value
ustate= document.form1.state.value
uzip= document.form1.zip.value
uphone= document.form1.phone.value
ufax= document.form1.fax.value
if (username="");
{
setCookie('username',username,365);
setCookie('lastname',lastname,365);
setCookie('stree1',stree1,365);
setCookie('stree2',stree2,365);
setCookie('ucity',ucity,365);
setCookie('ustate',ustate,365);
setCookie('uzip',uzip,365);
setCookie('uphone',uphone,365);
setCookie('ufax',ufax,365);
}
}
}

</script>

</head>
<body>
<h1>Marketing Seminar Registration</h1>
</br>
<form action="" name="form1">
First Name: <input type="text" size="15" name="fname">
Last Name: <input type="text" size="15" name="lname">
</br>
Street Address1: <input type="text" size="50" name="saddress1">
</br>
Street Address 2: <input type="text" size="50" name="saddress2">
</br>
City: <input type="text" size="30" name="city">
State: <input type="text" size="2" name="state">
Zip: <input type="text" size="5" name="zip">
</br>
Phone: <input type="text" size="15" name="phone">
Fax: <input type="text" size="15" name="fax">
</br>
Name you would like on your badge: <input type="text" size="30" name="bname">
</br></br>
<input type="button" name="submit" value="Submit" onclick="registerForm();">
</form>

<script type="text/javascript">

</script>

</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@Logic_AliNov 21.2010 — What is the "error console"?[/quote]

[URL]http://www.firefoxmastery.com/firefox/introducing-the-firefox-error-console/[/URL]
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 21.2010 — That is a helpful tool and thank for pointing that out to me but I actually use Internet Explorer so that doesn't really help.
Copy linkTweet thisAlerts:
@Logic_AliNov 21.2010 — That is a helpful tool and thank for pointing that out to me but I actually use Internet Explorer so that doesn't really help.[/quote]Internet Explorer's console (activated by clicking that yellow triangle bottom left) gives very poor error messages, which is why the cognoscente always develop code initially using other browsers.
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 21.2010 — I don't understand any of the error messages.
Copy linkTweet thisAlerts:
@Logic_AliNov 21.2010 — I don't understand any of the error messages.[/quote]The console will show errors from other defective sites you've visited during the session (like this one). Just click on [Clear], reload your page and you see only messages relating to your page.

Did you fix the error that I pointed out?
Copy linkTweet thisAlerts:
@jssupernoobauthorNov 22.2010 — Yeah, well I figured it out through trial and error. Thanks for your help.
×

Success!

Help @jssupernoob 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.21,
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,
)...