View Full Version : onSubmit="setCookie()" if cookie exists...

06-01-2005, 04:48 PM

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:

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:

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:


<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=/;"

I know I haven't defined the getCookie function as this eludes me too...


<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>');

Mr J
06-01-2005, 07:21 PM
How about something like this?

<TITLE>Document Title</TITLE>
<script type="text/javascript">

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
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 i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 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(offset, endstr));

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));



function get_cookie_data(){

// -->

<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">
<input name="tel" type="text" id="tel" size="16" class="inputf">
<input name="email" type="text" id="email" size="16" class="inputf">
<textarea name="address" cols="14" rows="2" class="inputf" id="address"></textarea>
<input type="submit" name="Submit" value="Submit" class="button1">
<p class="side">*Details are only used for items relating to LAL.</p></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>


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


06-02-2005, 02:41 PM
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 ? :p

Mr J
06-02-2005, 03:23 PM
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