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
    Jun 2005
    Location
    London
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onSubmit="setCookie()" if cookie exists...

    Hi,

    I am an absolute beginner at this and almost all of the stuff I read about cookies is about reading values back.

    I am trying to do this:
    Code:
    if (!getCookie) {
     document.write ('show user registration form with onsubmit="setCookie()"');
    } else {
     document.write ('some html showing user they have registered');
    }
    So I need to:
    Code:
    getCookie - if cookie exists show HTML with 'You are registered'
                  - if cookie doesn't exist show HTML with FORM
    setCookie - on the submission of the FORM
    Cookie should be valid for 365 days or so.

    The whole process is just eluding me and I would really appreciate some enlightenment as my efforts so far are simply to frustrating for words.

    Here it is just so you can understand what I am doing wrong:

    Head:
    Code:
    <script language="JavaScript">
    <!--
    var my_Days=365;                 // Expiry in days
    var my_Cookie='landasp';        // Cookie name
    
    function setCookie() {
    document.cookie=my_Cookie+"="+escape(value)+";expires="+new Date(new Date().getTime()+my_Days*86400000).toGMTString()+";path=/;"
    } 
    //-->
    </script>
    I know I haven't defined the getCookie function as this eludes me too...

    Body:
    Code:
    <script language="javascript">
        if (!getCookie("landasp")){
    	document.write ('<div class="register"><strong>REGISTER NOW</strong><br>');
        document.write ('<span class="side">For availability &amp; offers </span></div>');
        document.write ('<div class="register">');
        document.write ('<form action="cgi-bin/formProcess.pl" name="reserve" id="reserve" onsubmit="setCookie()">');
        document.write ('<input name="recipient" type="hidden" value="stewart@landaspirations.com">');
        document.write ('<input name="subject" type="hidden" value="Register Now">');
        document.write ('<input type="hidden" name="redirect" value="http://www.landaspirations.com/buying_test.htm">');
        document.write ('<input type="hidden" name="formtype" value="Register Now">');
        document.write ('<span class=\"side\">Name<br>');
        document.write ('<input name="name" type="text" id="name" size=16" class="inputf">');
        document.write ('<br>Tel<br>');
        document.write ('<input name="tel" type="text" id="tel" size="16" class="inputf">');
        document.write ('<br>Email<br>');
        document.write ('<input name="email" type="text" id="email" size="16" class="inputf">');
        document.write ('Address<br>');
        document.write ('<textarea name="address" cols="14" rows="2" class="inputf" id="address"></textarea>');
        document.write ('<br><br>');
        document.write ('<input type="submit" name="Submit" value="Submit" class="button1">');
        document.write ('</span>');
        document.write ('</form>');
    	document.write ('<p class="side">*Details are only used for items relating to LAL.</p></div>');
          } else {
     	document.write ('<div class="register">');
       	document.write ('<p><strong>THANK YOU FOR REGISTERING</strong></p>');
       	document.write ('<p class="side">*Details are only used for items relating to LAL.</span></p>');
     	document.write ('</div>');
    }
    	  </script>
    Last edited by Beeper; 06-01-2005 at 04:16 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    How about something like this?

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>Document Title</TITLE>
    <
    script type="text/javascript">
    <!--
    cookie_name="landasp"
    expdays=365

    // An adaptation of Dorcht's cookie functions.

    function set_cookie(namevalueexpirespathdomainsecure){
    if (!
    expires){expires = new Date()}
    document.cookie name "=" escape(value) + 
    ((
    expires == null) ? "" "; expires=" expires.toGMTString()) +
    ((
    path == null) ? "" "; path=" path) +
    ((
    domain == null) ? "" "; domain=" domain) +
    ((
    secure == null) ? "" "; secure");
    }

    function 
    get_cookie(name) {
    var 
    arg name "=";
    var 
    alen arg.length
    var 
    clen document.cookie.length;
    var 
    0
    while (
    clen) {
    var 
    alen;
    if (
    document.cookie.substring(ij) == arg){
    return 
    get_cookie_val(j); 
    }
    document.cookie.indexOf(" "i) + 1;
    if (
    == 0) break;
    }
    return 
    null;
    }

    function 
    get_cookie_val(offset){
    var 
    endstr document.cookie.indexOf (";"offset);
    if (
    endstr == -1)
    endstr document.cookie.length;
    return 
    unescape(document.cookie.substring(offsetendstr));
    }

    function 
    delete_cookie(name,path,domain){
    document.cookie name "=" +
    ((
    path == null) ? "" "; path=" path) +
    ((
    domain == null) ? "" "; domain=" domain) +
    "; expires=Thu, 01-Jan-00 00:00:01 GMT";
    }

    function 
    saving_cookie(){
    var 
    expdate = new Date ();
    expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000));

    Data="cooked"

    set_cookie(cookie_name,Data,expdate)
    }

    function 
    get_cookie_data(){
    inf=get_cookie(cookie_name)
    if(!
    inf){
    document.getElementById("display1").style.display="block"
    }
    else{
    document.getElementById("display2").style.display="block"
    }
    }

    // -->
    </script> 


    </HEAD>
    <BODY onload="get_cookie_data()">

    <div id="display1" style="display:none">
    <div class="register"><strong>REGISTER NOW</strong><br>
    <span class="side">For availability &amp; offers </span></div>
    <div class="register">
    <form action="cgi-bin/formProcess.pl" name="reserve" id="reserve" onsubmit="saving_cookie()">
    <input name="recipient" type="hidden" value="stewart@landaspirations.com">
    <input name="subject" type="hidden" value="Register Now">
    <input type="hidden" name="redirect" value="http://www.landaspirations.com/buying_test.htm">
    <input type="hidden" name="formtype" value="Register Now">
    <span class=\"side\">Name<br>
    <input name="name" type="text" id="name" size=16" class="inputf">
    <br>Tel<br>
    <input name="tel" type="text" id="tel" size="16" class="inputf">
    <br>Email<br>
    <input name="email" type="text" id="email" size="16" class="inputf">
    Address<br>
    <textarea name="address" cols="14" rows="2" class="inputf" id="address"></textarea>
    <br><br>
    <input type="submit" name="Submit" value="Submit" class="button1">
    </span>
    </form>
    <p class="side">*Details are only used for items relating to LAL.</p></div>
    </div>

    <div id="display2" style="display:none">
    <div class="register">
    <p><strong>THANK YOU FOR REGISTERING</strong></p>
    <p class="side">*Details are only used for items relating to LAL.</span></p>
    </div>

    </div>

    <P>For testing <a href="#null" onclick="delete_cookie(cookie_name)">Delete Cookie</a>

    </BODY>
    </HTML> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Jun 2005
    Location
    London
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    wow! fantastic

    Thank you so much I won't pretent to understand much of it but I see that it checks to see if there is a cookie and then determines wether to show display1 or display2 which are not shown by default

    Now if I wanted to have a set of 8 forms and each one set a variable or I thought like this:

    varforms = 00000000 // none of the eight forms are completed

    varforms = 00100000 // the third from is completed

    varforms = 01110001 // forms 2,3,4 and 8 are completed

    how would I set and retrieve the variable for each form.

    explode ??

    then I want to say show the first uncompleted form or show the first of form 5,6 or 7 that isnt complete ...

    or am I getting in way over my head ?

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I suppose it could be done by the use of an array containing an index for each form where the values are initially set to zero and changed to one if a form is complete.

    Then the array is checked and the relevant index value denotes if the form is complete.

    I'll give this some more thought later
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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