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

getCookie from form for redirect page 1

Status
Not open for further replies.

Jeanne

Technical User
Jan 15, 2001
45
US
Hi. I am trying to use a cookie to get a name entered in a form to show on the redirect page.

I am using a Mac, with email.cgi for the form

The problem I am having is that "undefined" is showing on the redirect page instead of the name.

I am using the following script:

ON FORM PAGE:


<SCRIPT LANGUAGE=&quot;JavaScript&quot;><!--
function setCookie(NameOfCookie, value) {
document.cookie = NameOfCookie + &quot;=&quot; + escape(value)
}

function next() {
if (document.forms[0].name.value==&quot;&quot;) {
alert(&quot;You didn't enter a name.&quot;)
return false
}
else {
setCookie('Name',document.forms[0].name.value)
window.location=&quot;calvertdirect.html&quot;
}
} //-->
</SCRIPT>

IN FORM:

<input type=&quot;text&quot; name=&quot;name&quot; maxlength=&quot;80&quot; size=&quot;15&quot;>

<input type=&quot;text&quot; name=&quot;from_address&quot; maxlength=&quot;80&quot; size=&quot;15&quot;>
<input onclick=&quot;return next()&quot; type=&quot;submit&quot; name=&quot;Go&quot; value=&quot;Go&quot;>

ON REDIRECT PAGE:


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function getCookie(NameOfCookie){
if (document.cookie.length > 0) {
begin = document.cookie.indexOf(NameofCookie+&quot;=&quot;)
if (begin != -1) {
begin += NameOfCookie.length+1;
end = document.cookie.indexOf(&quot;;&quot;, begin)
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(begin, end))
}
}
return &quot;&quot;
}

var Name=getCookie('Name')
//-->
</SCRIPT>

<STYLE TYPE=&quot;TEXT/CSS&quot;>
.name {
font-family: Verdana;
font-size: 16px;
position: absolute;


}
</STYLE>

IN BODY:

<span class=&quot;name&quot; style=&quot;left: 205px; top: 320px&quot;></span>
<p align=&quot;left&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot; size=&quot;3&quot; color=&quot;#000000&quot;>
<script language=&quot;javascript&quot;>
<!--
document.write(Name)
//-->
</script>

I am sure this is a simple error, because I don't know much about JavaScript. I would very much appreciate any help you could give me with this.

Thanks,

Jeanne
 
well...I don't really follow your script that gets the cookie, probably because I don't work with strings much, but first try naming your form (put name=&quot;whatever&quot;) in the <form> tag, then call it with document.formName.elementName.value this is probably not the problem, but you never know..since I don't know what browser you are using, give it a try...Then if that doesn't help try using this to get your cookie back make sure you declare the variables outside the function:

Code:
var thisCookie, cookieName, cookieVal

if (document.cookie != &quot;&quot;) {

     thisCookie = document.cookie.split(&quot;; &quot;)
 
     for (i=0; i<thisCookie.length; i++) {

          if (thisCookie[i].split(&quot;=&quot;)[0] == &quot;Name&quot;) {
               cookieName = thisCookie[i].split(&quot;=&quot;)[0]
               cookieVal = thisCookie[i].split(&quot;=&quot;)[1]
          }
     }
}

document.write(cookieName + &quot;=&quot; + cookieVal)

also..you should make the cookie with an expiration date like this:

[code]
expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+3)

then the cookie would be:

document.cookie = NameOfCookie + &quot;=&quot; + escape(value) + &quot;;expires=&quot; + expireDate.toGMTString()

to delete the cookie just do this:

Code:
deleteDate = new Date
deleteDate.setMonth(expireDate.getMonth()-1)

document.cookie = NameOfCookie + &quot;=null;expires=&quot; + deleteDate.toGMTString()
[/code]
-Greg :-Q
 
oops..forgot about the escaping..unescape the value like this:

Code:
cookieVal = unescape(cookieVal)

put this right after the loop closes...(after 2nd brace)
-Greg :-Q
 
Thanks for your help! I will try it.

I need this to work on all the major browsers.

I'll let you know how it works out.

Jeanne
 
Okay, I really don't know what I am doing.

How do I &quot;call it with document.formName.elementName.value&quot; ?

Will I put this in the script that I already have? Or will I make a new script for that?

Thanks,

Jeanne
 
Actually you probably don't have to do the whole document.formname.elementname.value thing... if the form you are getting the information from is the first on the page document.forms[0].elementname.value will work fine in all browsers.
 
I only have one form on the page, in which I ask for the person's name, company name, interests and email address. Thanks.
 
Well, I was right. It was a very simple mistake.
In one of the places that had &quot;NameOfCookie&quot; I had written &quot;NameofCookie&quot;
It seems to be working fine now.

Thank you very much for your help Greg and aperfectcircle!

Jeanne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top