Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies Code

Status
Not open for further replies.

Dummy99

Programmer
Apr 25, 2002
30
0
0
FR
I have been trying to create & use a cookie to remember the user's ID. All the different recomended coding I've found doesn't work - or I don't understand cookies!

By playing around with the code, it seems I managed to set up a cookie. But, I can't delete it or set up a new one. Furthermore the cookie that was seemingly set up doesn't givee me the necessary info.

Everytime I run the code the cookie displayed is always the same: "RemID; UserID=yes". No matter what I do I always get this.

My latest code follows below.

Can you please help me?
Thank you.

<html>
<head>
<title>Log In</title>
<!-- ============================================= -->
</head>
<body onload="mystart()">
<form name="ET_Login" action="ET_HomePage.html" method="POST" onsubmit="mysubmit()">
<!-- ===================== JavaScript ======================== -->
<script type="text/javascript">
//=============================================
function mystart()
{document.getElementById('UserID').focus();
getCookie("RemUserID");
alert(document.cookie) // <--for degugging only.
if (document.cookie.length == 0)
{}
else {document.getElementById("UserID").value = document.cookie.substring(0,(document.cookie.length));
document.getElementById('pswd').focus();}}
//=============================================
function mysubmit()
{if (document.getElementById("RemBox").checked)
{createCookie("RemUserID", document.getElementById("UserID").value, "1");}
else {alert("go erase")
eraseCookie("UserID");}}
//=============================================
function createCookie(name, value, days)
{if (days)
{var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();}
else {var expires = "";
document.cookie = name+"="+value+expires+"; path=/";}}
//=============================================
function getCookie(name)
{var ca = document.cookie.split(';');
var nameEQ = name + "=";
for(var i=0; i < ca.length; i++)
{var c = ca;
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0)
{return c.substring(nameEQ.length, c.length);}}
return null;}
//=============================================
function deleteCookie(name)
{createCookie(name, "", -1);}
//=============================================
</script>
<noscript>Your browser does not support JavaScript!</noscript>
<!-- =================================================================== -->
<h1 align=center><i><b>
User ID:
<input name="UserID" size="15" maxlength="15">
<br>
Password:
<input type="password" name="pswd" size="15" maxlength="15">
<br>
<input type="checkbox" name="RemBox" checked><font size="2">Remember my User ID.</font>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
 
[1] Name is not the universal fallback in the absence of id. Hence, you have to add id attributes to those input controls.

[tt]<input [red]id="UserID"[/red] name="UserID" size="15" maxlength="15">
<input type="password" [red]id="pswd"[/red] name="pswd" size="15" maxlength="15">
<input type="checkbox" [red]id="RemBox"[/red] name="RemBox" checked><font size="2">Remember my User ID.</font>
[/tt]

[2] You have a misplaced closing bracket in createCookie hindering cookie's setup.
[tt]
else {var expires = ""[highlight]}[/highlight];
document.cookie = name+"="+value+expires+"; path=/";[highlight] [/highlight]}
[/tt]
 
Tsuji,
I have made the changes you stated but I still get the same results, i.e., the cookie displayed is always the same: "RemID; UserID=yes", even if I enter a new user id.
There must be some other problem.

Something else you should perhaps know. I am in development stage only and so I am running this on my PC only; no server. Does that make any difference? If so, why? My understanding is the cookie is stored at the PC so server or no server, the code should work.

Awaiting your reply with thanks.
 
For testing purpose as you have an onload handling, the easiest is to change the action attribute to the of the page itself (ie submitting to itself) and the effect of the cookie should then show up.
 
With another browsing through the script, I think you have to clean it up a little/a lot. Your onsubmit handler probably error out somewhere and does not perform well. For instance, I see a call to eraseCookie function, but there isn't any function of that name---there is a deleteCookie() etc... With an erronous onsubmit handler, you cannot expect things all get done as expected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top