Cookies and Forms

Trying to figure out how to keep a form from submitting when a cookie

Page 1 of 1

2 Replies - 718 Views - Last Post: 23 May 2009 - 09:36 PM

#1 skwid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 31-March 09

Cookies and Forms

Post icon  Posted 21 May 2009 - 07:33 PM

Hi everyone here is my question: What I have is a form that validates that there is information in it, after it has checked the form and before it submits the information it will check to see if a cookie is on the users computer, then it either sets a cookie, or it tells the user that they have already submitted a form. The problem I am having is it submits the form whether or not there is a cookie set, and I am trying to get it to stop trying to submit the form if there is a cookie on the users computer. I have tried different ways to stop it, but when I am able to stop it, it also will not open the window that tells the user they have already submitted the form, I'm wondering if someone could look at my code and explain what I am doing wrong, thanks. This is just a test form that I use so it is not fancy:

<html>

<head>

<title>test page 3</title>

<script type="text/javascript">

function validate_req (field,alerttxt) 
{

with (field)
  {
  if (value==null||value=="")
	{
	alert(alerttxt);
	return false;
	}
  
	return true;

  }
}

function validate_form(thisform)
{
with (thisform)
  { 
		  if (!validate_req(mail,"Your email is required.")) { mail.focus(); return false; }
		  if (!validate_req(name,"Your name is required.")) { name.focus(); return false; }

  }
 
  checkCookie()	
  return true;

}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{
	c_start=c_start + c_name.length+1;
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	}
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {

	  window.open("submitted2.htm","_blank","width=400, height=200");

 /* most of what I have tried to stop the form from submitting I tried here such as return false; document.action="null"; break; etc. */

  }
else
  {
  username="name";
  if (username!=null && username!="")
	{
	setCookie('username',username,365);
	}

	window.open("submited.htm","_blank","width=400, height=200");

  }
}

</script>

</head>

<body>
<h1 text-align="center">Test Form 3</h1>

<hr>

<form action="mailto:" onsubmit="return validate_form(this)" method="post">

<h3>This form sends an e-mail to W3Schools.</h3>
Name:<br>
<input type="text" name="name" size="20">
<br>
Mail:<br>
<input type="text" name="mail" size="20">
<br>
Comment:<br>
<input type="text" name="comment" size="40">
<br><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">

</form>

</body>

</html>


Thanks, any help would be appreciated.

This post has been edited by skwid: 21 May 2009 - 08:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cookies and Forms

#2 roudard  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 143
  • Joined: 26-September 05

Re: Cookies and Forms

Posted 23 May 2009 - 01:12 AM

this function always returns true ...

function validate_form(thisform)
{
with (thisform)
  { 
		  if (!validate_req(mail,"Your email is required.")) { mail.focus(); return false; }
		  if (!validate_req(name,"Your name is required.")) { name.focus(); return false; }

  }

  checkCookie()	
  return true;

}



modifiy checkCookie so it returns true or false, and change the return of validate_form() to

return checkCookie() ;

that should do it

Hope this helps
Was This Post Helpful? 1
  • +
  • -

#3 skwid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 31-March 09

Re: Cookies and Forms

Posted 23 May 2009 - 09:36 PM

It helped greatly, I knew I needed to add a return true and false, but I didn't think about removing the return true from the validate_form section.

Thanks, you guys here at dream in code are very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1