The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How would I set a cookie that remembers an e-mail address that somebody supplied on a certain form, and calling it up later?

Here is what I'm trying to do...
I'm using a seperate script that handle my newsletter subscribers. I want it to set a cookie when somebody presses submit, because it then processes the e-mail with a CGI script on a seperate server, then redirects to a thank you page on my server. I would have no problem doing this, but the seperate server it goes through is really interfering. On the thank you page, I want it to have the e-mail in another subscribe box for an affiliate so all they have to do is click 'Submit'.

As long as you're setting and reading the cookie on your domain, you should be ok. You can't read or write cookies from other domains. Also, don't set a path for your cookie. By default, the path is "/", allowing all pages on your server to access the cookie. I used this with ASP, so it automatically wrote a session ID to the cookie, but if the only value in the cookie is your user's email address, change two lines in the JavaScript:

If I understood, your site hands off a form to a server outside your web site that handles the CGI and the 'Thank You' page. This is a condition which makes it really tough if not impossible to access cookies from the alternate server based 'Thank You' page. The security system is designed to prohibit this. With P3P coming in IE6 it will be even more difficult.

I would suggest that your CGI program direct them back to a web page on your site for the 'Thank You' page. The page on your site could then read the cookie easily. A client side JS could read the cookie and load a form variable for the submit.

An alternative if you HAVE TO process the 'Thank You' page on the alternate server would be to call the page with the address loaded in the URL (ie, somewhere.com/thankyou.html?theaddress@their.com).

But in any case, there is no reason to add pound/sharp signs. Also, if you want to check the cookie quickly, as it will be accessed by _Javascript_ (how the browser stores it on your harddrive is irrelevant), type in javascript:alert(document.cookie) into your location bar.

Silverado4x4,
I haven't been following this closely. Forgive me if I an out of sequence or missing the current point...

A quick look at the code of SetCookie() shows there is no RETURN value (at least as it is shown here). This will cause the form submission problems. Return either true, to continue the submission process, or false, to abort the process and remain on the page.

Plus there are several JS and VB include files that handle cookies. Most of these have all the encode/decode handled inside the routine. All you need to do is set the name and value. Request the cookie name and save it to a varable for use. Grab one of those to use with client side processes and take the routines from them for your server side.

There is no need to be down to SPLIT for pulling cookie info. All this is handled in typical routines like CookieSet( "cookiename", "cookievalue", "expiredate") which allows you to build plain text strings. Just look for a routine that encodes the data. Its corresponding routine CookieGet( "cookiename) will return the value and decode it. Search at Google or iLOR for java script cookies.

Good point about the return value, but he doesn't need any other encoding function: he already has one. And while you're correct that you don't _have_ to use split, it works fine. Using a non-split method of accessing the cookie doesn't make it any better, because splitting will work fine in this application.

Silverado4x4
You are doing a lot of work. Part of the problem is hard coding the [] indexes. Programmers talk about what level one works at. You're working at too low a level. If you hard code those indexes, any change in the cookie will through them off. If the code is moved to a Windows server with ASP form a UNIX box without it, the cookie format can change. Which will break the code. Adding another varable to the cookie will break the code. Some of my manuals point out that the order of values in a cookie cannot be relied on. I have never tested that, but I also do not rely on the values to be in a particular order. Use the get cookie routine I provided to grab the value you want by name. Save yourself some work.

This is way easier if you use the calls to getCookie and setCookie like I provided. They figure out the index for you and if it changes no big deal.

Your <form> tag in page1 needs to be written so the OnSubmit looks like this: onSubmit="return setCookie( 'nameofyourchoice', 'theEmailAddress', 'expires.... ')"

Notice the use of the word 'return'.

In your function setCookie() the last line should be: return true;

If you want to stop processing, say when the e-mail validation fails, return false;