On form submit set cookie

Hi,

I want to set a cookie when a form is submitted before it goes off to the formprocessor.cgi which is not ours to modify.

I have been kindly given some code to set the cookie but I am looking for how I attach it to a form submission.

In the head I have placed :

<script language="JavaScript">
<!--
var pv_Days=365;                    // The Links will be remembered on revisits for a specified number of days

var pv_Cookie='landasp';        // The Cookie name

function pv_SetCookie() {
document.cookie=pv_Cookie+"="+escape(value)+";expires="+new Date(new Date().getTime()+pv_Days*86400000).toGMTString()+";path=/;"
}
//-->
</script>

and then in tha body I wrote:

<script language="javascript">
    if (!pv_GetCookie()){
	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=\\"http://www.grow-fresh.com/cgi-bin/formProcess.pl\\" name=\\"reserve\\" id=\\"reserve\\" onsubmit=\\"pv_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.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>');
    document.write ('Tel<br>');
    document.write ('<input name=\\"tel\\" type=\\"text\\" id=\\"tel\\" size=\\"16\\" class=\\"inputf\\">');
    document.write ('<br>');
    document.write ('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 ('<span class=\\"side\\">*Details are only used for items relating to LAL.</span> </div>');
      } else {
document.write ('<div class="register">');
 document.write ('<strong>REGISTER NOW!*</strong><br>');
   document.write ('<span class="side">For availability &amp; offers</span></div>');
 document.write ('<div class="register">');
   document.write ('<p><strong>THANK YOU FOR REGISTERING WITH LAND ASPIRATIONS.</strong></p>');
   document.write ('<p> <span class="side">*Details are only used for items relating to LAL.</span> </p>');
 document.write ('</div>');
}
	  </script>

What I expected was that I would see the form. Then once I submitted it I wouldn’t see it again as the cookie would be set. In Firefox javascript console I get : “pv_getCookie is not defined” how do i define it?

That cookie script looks dubious… In any case, you’re not calling the function. It needs to be:

onsubmit=\\"pv_SetCookie[color=blue]()[/color]\\"

BTW, you should use server-side scripting rather than document.write, since cookies can also be accessed server-side.

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

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@pob.com">
<input name="subject" type="hidden" value="Register Now">
<input type="hidden" name="redirect" value="http://www.pob.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>REGISTERED</strong></p>
</div>

</div>

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

</BODY>
</HTML>