SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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'.

    Hope this doesn't sound too complicated...

    Thanks in advance!

  2. #2
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    userInfo = document.cookie.split("\;");
    userEmail = userInfo[0].split("\=");

    to this

    userInfo = document.cookie.split("\=");
    userEmail = userInfo[1];

    Whew! With that in mind, here's some code:

    The first page:
    Code:
    <html>
    <head>
    <title>Page 1</title>
    
    <script language="JavaScript">
    <!-- 
    
    function Set_Cookie(name,value,expires,path,domain,secure) 
    { document.cookie = name + "=" +escape(value) + ( (expires) ? ";expires=" + expires: "") + ( (path) ? ";path=" + path : "") + ( (domain) ? ";domain=" + domain : "") + ( (secure) ? ";secure" : ""); 
    }
    
    // -->
    </script>
    </head>
    
    <body>
    
    <form name="myForm" action="" method="post" onSubmit="Set_Cookie('userEmail',document.myForm.emailAddress.value,'Mon, 20-Feb-2012 00:00:01 GMT')">
    <input type="text" name="emailAddress">
    <input type="submit" name="submit" value="Submit">
    </form>
    
    <a href="page2.html">Check cookie</a>
    
    </body>
    </html>
    Page 2:
    Code:
    <html>
    <head>
    <title>Page 2</title>
    
    <script language="JavaScript">
    <!-- 
    
    function populateEmail() {
    userInfo = document.cookie.split("\;");
    userEmail = userInfo[0].split("\=");
    document.form2.email2.value = userEmail[1];
    
    }
    // -->
    </script>
    </head>
    
    <body onLoad="populateEmail();">
    
    <form name="form2" action="" method="post">
    <input type="text" name="email2">
    <input type="submit" name="submit" value="Submit">
    </form>
    
    </body>
    </html>

  3. #3
    SitePoint Enthusiast Setac's Avatar
    Join Date
    Nov 2000
    Location
    San Marcos CA
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

    Alan
    Dynamic HTML - Is that a Frisbee based language...

  4. #4
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I will try it out.

    Setac: The thank you page is on MY server, but the rest is correct.

  5. #5
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    scrubz:

    I think we have something going here, but it comes up with the cookie as "undefined".

    http://www.jokes-central.com/page1.html

  6. #6
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bummer. Yeah, I looked at the cookie it's creating, and here's the way the cookie is written by your server:

    userEmail
    test@test.com
    www.jokes-central.com/
    0
    2537264768
    30207842
    435122560
    29414265
    *

    This is slightly odd to me, since when writing the cookie, it tells it specifically to put it in the format "userEmail=test@test.com", but there's no = sign in the cookie.

    So, with that cookie, it's not correctly pulling the email address out. I'll see if I can come up with a semi-easy way to get the email address out of that cookie.

  7. #7
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Location
    Van down by the river
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...

    Change the onSubmit line to this:

    onSubmit="Set_Cookie('userEmail#',document.myForm.emailAddress.value + "#",'Mon, 20-Feb-2012 00:00:01 GMT')">

    And replace the first two lines of the populateEmail function with this:

    userInfo = document.cookie.split("#");
    userEmail = userInfo[1];

  8. #8
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help, but it now it jsut leaves the box blank on the second page!

    http://www.jokes-central.com/page1.html

  9. #9
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.myForm.emailAddress.value + "#"
    should be:
    document.myForm.emailAddress.value + '#'
    which is why it's giving a javascript error.

    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.

  10. #10
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still won't work dadgumit! lol

    http://www.jokes-central.com/page1.html

  11. #11
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The source is exactly the same as the old version.

  12. #12
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops! I edited the wrong thing.

    Still, it says "undefined". Arghh

    http://www.jokes-central.com/page1.html

  13. #13
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take out the pound signs also and split using ";" then split index 2 using "=", then read index 1.

  14. #14
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know zilch about javascript, so if you don't mind, can you show me how to do it like step-by-step?

  15. #15
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First change your onSubmit to:
    onSubmit="Set_Cookie('userEmail',document.myForm.emailAddress.value,'Mon, 20-Feb-2012 00:00:01 GMT')"

    then on the second page make it:


    function populateEmail() {
    cookies = document.cookie.split(";");
    email = cookies[2].split("="); // index = 2
    document.form2.email2.value = email[1]; // index = 1

    }

    If you type javascript:alert(document.cookie) into the address bar, it'll help you see how this works.

  16. #16
    SitePoint Enthusiast Setac's Avatar
    Join Date
    Nov 2000
    Location
    San Marcos CA
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Or use this one: (clean up the line breaks)
    <!--
    //========== This page's Cookies =======================
    function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {
    begin = document.cookie.indexOf(NameOfCookie+"=");
    if (begin != -1) { // Note: != means "is not equal to"
    begin += NameOfCookie.length+1;
    end = document.cookie.indexOf(";", begin);
    if (end == -1) end = document.cookie.length;
    //alert("Found cookie: " + NameOfCookie)
    return unescape(document.cookie.substring(begin, end));
    }
    }
    //alert("No " + NameOfCookie + " cookie found");
    return null; //No cookie
    }

    function setCookie(NameOfCookie, value, expiredays) {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
    document.cookie =
    NameOfCookie + "=" + escape(value) +
    ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
    }

    function delCookie (NameOfCookie){
    if (getCookie(NameOfCookie)) {
    document.cookie = NameOfCookie + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
    }
    //=============================================================
    //-->
    Dynamic HTML - Is that a Frisbee based language...

  17. #17
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  18. #18
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now it gives me different numbers!

    http://www.jokes-central.com/page1.html

  19. #19
    SitePoint Enthusiast Setac's Avatar
    Join Date
    Nov 2000
    Location
    San Marcos CA
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;

    Alan
    Dynamic HTML - Is that a Frisbee based language...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •