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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

??? Cookie error <'email' is underfined> ???

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I’m trying to get some cookies set up on my but, like so many others; I haven’t a clue what I’m doing! I’ve tried to edit a script I found down the back of my sofa (it’s always the first place I look), but with no luck.
The fields I’d like the cookie to remember are: name, email, url, url_title

I keep getting this error: Error: 'email' is underfined

I've put the script just after this:
<form method=POST name=&quot;MyForm&quot; action=&quot;
You can view the board here (This test board's not working yet, but I'm just working on the cookies at the moment).

Does anybody know what I'm doing wrong?
Code:
<SCRIPT LANGUAGE = &quot;JavaScript&quot;>
<!-- 

// Use this function to retrieve a cookie.
function getCookie(name){
var cname = name + &quot;=&quot;;               
var dc = document.cookie;             
    if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
        if (begin != -1) {           
        begin += cname.length;       
        end = dc.indexOf(&quot;;&quot;, begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        } 
    }
return null;
}

// Use this function to save a cookie.
function setCookie(name, value, expires) {
document.cookie = name + &quot;=&quot; + escape(value) + &quot;; path=/&quot; +
((expires == null) ? &quot;&quot; : &quot;; expires=&quot; + expires.toGMTString());
}

// Use this function to delete a cookie.
function delCookie(name) {
document.cookie = name + &quot;=; expires=Thu, 01-Jan-70 00:00:01 GMT&quot; +  &quot;; path=/&quot;;
}

var exp = new Date();                                   // make new date object
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 31)); // set it 31 days ahead

with (document.MyForm) {
setCookie(&quot;name&quot;, name.value, exp);
setCookie(&quot;email&quot;, email.value, exp);
setCookie(&quot;url&quot;, url.value, exp);
setCookie(&quot;url_title&quot;, url_title.value, exp); // save the cookie
}

document.write(getCookie(&quot;name&quot;) + &quot;<br>&quot; +
getCookie(&quot;email&quot;) + &quot;<br>&quot; +
getCookie(&quot;url&quot;) + &quot;<br>&quot; +
getCookie(&quot;url_title&quot;)); // retrieve and display cookie on page

delCookie(&quot;name&quot;);
delCookie(&quot;email&quot;);
delCookie(&quot;url&quot;);
delCookie(&quot;url_title&quot;); // delete cookie

//-->
</SCRIPT>
 
--> please don't post a whole page of code - only the relevant parts (here those would be : the line where you get the value - the line where you SET the email and the line where you GET it) - this would also help if you tell us what you did to debug :
try to alert email.value in the with document.form part : is it the correct one ?
try to read (edit it) the cookie : is there a value for &quot;email&quot; ?
try to do a very simple cookie, write it and retrive it and read it : is this working ?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top