...

View Full Version : Newbie needs help



bigmex
11-07-2006, 09:52 AM
I need help with this code. I am trying to add a cookie to this form so that when a user completes the form and sends, if they attempt to fill it out again they are directed to an html page stating they already filled out the form. Any help? thanks.



<html>
<head>

<title>My form</title>

<script language="JavaScript">
<!-- Hide the following code
function openDefinitionPage(URL) {
myWindow=window.open(URL,"definition","toolbar=no,width=250, height=250,scrollbars=yes,resize=yes");
}

function validate()
{
if (document.mlist.fname.value=="")
{
alert ("You must fill in all of the required fields!")
return false
}
}

function validate()
{
if (document.mlist.lname.value=="")
{
alert ("You must fill in all of the required fields!")
return false
}
}

function validate()
{
if (document.mlist.addre.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}

function validate()
{
if (document.mlist.city.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}

function validate()
{
if (document.mlist.state.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}

function validate()
{
if (document.mlist.phone.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}

function validate()
{
if (document.mlist.email.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}

function validate()
{
if (document.mlist.comm.value=="")
{
alert ("You must fill in all the required fields!")
return false
}
}
// End the hiding here -->
</script>

</head>
<body>
<h1>Join our mailing List!</h1>

*All fields required


<form method="get" name="mlist" onsubmit="return validate()">

<table>
<tr>

<td>First Name:</td>
<td>

<input type="text" name="fname" size="25" maxlength="25" />

</td>
</tr>
<tr>
<td>Last Name:</td>
<td>

<input type="text" name="lname" size="40" maxlength="40" />

</td>
</tr>

<tr>
<td>Address:</td>
<td>

<input type="text" name="addre" size="50" maxlength="50" />

</td>
</tr>


<tr>

<td>City:</td>
<td>

<input type="text" name="city" size="40" maxlength="40" />

</td>
</tr>
<tr>
<td>State:</td>
<td>
<input type="text" name="state" size="2" maxlength="2" />
</td>
</tr>

<tr>

<td>Phone:</td>

<td>

<input type="text" name="phone" size="10" maxlength="10"/>



</td>
</tr>

<tr>

<td>E-mail Address:</td>

<td>

<input type="text" name="email" size="40" maxlength="40" />

</td>

</tr>

<tr>
<td>Comments:</td>
<td>

<textarea name="comm" cols="40" rows="8"></textarea>
</td>
</tr>


<tr>
<td>&nbsp;</td>
<td>


<input type="submit" value="Click here to send" />
</td>
</tr>

<tr>
<td>&nbsp;</td>
<td>

<input type="reset" value="Reset form" />
</td>
</tr>

</table>

</form>

</body>

</html>

Philip M
11-07-2006, 08:05 PM
For cookies see:-

http://www.htmlgoodies.com/beyond/javascript/article.php/3470821

You cannot have multiple functions with the same name (validate).
You could overcome this by renaming them validate1, validate2 etc. but you can see that they are very repetitious and can be greatly condensed.

e.g.

if (document.mlist.fname.value=="") || (document.mlist.lname.value=="") ||
(document.mlist.addre.value=="") .................


Another problem is that

if (document.mlist.fname.value=="")
simply checks for a blank field, but any character, even a space, will
pass the "validation" which is hardly worth doing. For example, a name ought to contain at least three letters and no numbers or punctuation. A phone number must be all numerals possibly with - or space as spacers. Only certain letter combinations for US States are valid. An email address must conform to a particular pattern etc. etc.

In other words, much more extensive validation of the various fields is needed if the validation is to have any real utility.

Also, a guy may have filled out the form, then realised that he made a (spelling?) mistake, and wants to do it again. Your cookie plan will frustrate that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum