Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2010
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    cookie content always "undefined"

    Hello. My goal is to write a form input to the cookie. The rest of the cookie works, and if I declare var user_email = 'muppet';, the content becomes "muppet". However, if I try to do something more dynamic, like var user_email = document.addform.email it becomes "undefined". I tried adding ".value" to that as well. I have been at this since yesterday, and tried solutions like running a setEmail() function, or getElementById.

    Here is my cookie writing function, which I call with onsubmit="createCookie('userEmail',user_email,365)"
    Code:
    <script type="text/javascript">
    
    var user_email = 'muppet';
    
    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=/";
    }
    </script>
    The form looks like this:
    Code:
    <form name="addform" action="http://formmail.dreamhost.com/cgi-bin/formmail.cgi" method="post" onsubmit="createCookie('userEmail',user_email,365)">
    <input type="hidden" name="required" value="email" />
    <input type="hidden" name="redirect" value="http://www.saverally.com/confirmed.html" />
    <input type="hidden" name="recipient" value="addme@saverally.com" />
    <input type="hidden" name="subject" value="add to list" />
    Your Email: <input type="text" name="email" name=email size="20" value="" />
    <input type="submit" value="Send"/>
    </form>
    Thanks!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,422 Times in 4,387 Posts
    And where do you ever define the variable user_email ????

    I sure don't see it any place.

    Is user_email supposed to be the value of *this* form field:
    Code:
    Your Email: <input type="text" name="email" name=email size="20" value="" />
    ?????

    (Incidentally, you have incorrectly twice specified the name= for that field. Only the first is correct, with the quote marks.)

    If so, then you should be getting *THAT* value:
    Code:
    <form name="addform" 
          action="http://formmail.dreamhost.com/cgi-bin/formmail.cgi" 
          method="post" 
          onsubmit="createCookie('userEmail',this.email.value,365); return true;">
    Though I would think you would want to validate that value to be a valid email address before storing it in the cookie.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jul 2010
    Posts
    61
    Thanks
    0
    Thanked 21 Times in 21 Posts
    Within the scope of a form, its fields are addressable by name, so all you need is:

    onsubmit="createCookie('userEmail', email.value, 365)"

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,422 Times in 4,387 Posts
    Quote Originally Posted by Phil Ippem View Post
    Within the scope of a form, its fields are addressable by name,
    True, but (a) the this doesn't hurt anything and (b) I find that it helps clarify the intent of the code.

    In any case, the important part is that his user_email is never defined.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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