...

View Full Version : contact form with validation



duggins
02-03-2004, 08:31 PM
have a contact form with validation wanting to use drop down box to pass the info to different php files.. something isn't right.



<html>
<head>
<title>Contact us at Casair</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function formHandler(f){
if (f.site.selectedIndex>0) {
f.action=f.site.options[f.site.selectedIndex].value;
f.submit();
}
}
// End -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</SCRIPT>
</head>
<body bgcolor="#000000">
<form action="" method="post" name="form" onSubmit="MM_validateForm('name','','R','address','','R','state','','R','zip','','RisNum','phone','','RisNum', 'email','','RisEmail');return document.MM_returnValue">
<table width="200" border="0">
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Full
Name</font></td>
<td><fieldset>
<input name="name" type="text" id="name" size="35">
</fieldset></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Company</font></td>
<td><input name="company" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Address</font></td>
<td><input name="address" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">State</font></td>
<td><input name="state" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Zip</font></td>
<td><input name="zip" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Phone</font></td>
<td><input name="phone" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Fax</font></td>
<td><input name="fax" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Email</font></td>
<td><input name="email" type="text" size="35"></td>
</tr>
<tr>
<td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Department</font></td>
<td><select name="site" ID=Select2>
<option value="http://www.4dn.com/casair/html/sales.php">Sales
<option value="support.php">Support </select></td>
</tr>
<tr>
<td valign="top"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Comments</font></td>
<td><textarea name="textarea" cols="26" rows="8"></textarea></td>
</tr>
<tr>
<td><input type="button" value="Submit" onclick="formHandler(this.form)"></td>
<td><div align="right">
<input type="button" value="Close" onClick="window.close()">
</div></td>
</tr>
</table>
</form>
</body>
</html>

RadarBob
02-03-2004, 09:32 PM
I didn't study your code, or try to run it; but boolean operators are always suspect. Forgive me if I'm preaching to the choir.

Things to keep in mind:
Javascript defines as true anything that is not 0(zero), ""(empty string), null, undefined, false.

&& - "shortcut" AND operator. If the LEFT side evaluates to 'false' then the right side is never evaluated (executed!), because the expression can't be true.

|| - "shortcut" OR operator. If the LEFT side evaluates to 'true', the expression is true, so it doesn't matter what the right side is, thus the right side is never evaluated.

Logical operator precidence is:
! && ||

further, ! is a unary operator.

thus:
! x && y
will evaluate like this:
(!x) && y

if
x = true; y=false;
then
!x && y is false
!(x && y) is true

duggins
02-03-2004, 10:28 PM
to be honest i have no idea what you just said lol i have a feeling the form isn't working because the submit button is pointing to a javascript thus the validation won't run because theres no form submit, but this doesn't explain why the redirect to the php files isn't working.

glenngv
02-04-2004, 08:40 AM
Calling formReference.submit() doesn't trigger the onload event handler. Don't ask me why, I don't know. :p

So you must call the onsubmit explicitly.


function formHandler(f){
if (f.site.selectedIndex>0) {
f.action=f.site.options[f.site.selectedIndex].value;
f.onsubmit();
if (!document.MM_returnValue) return false;
f.submit();
}
}

duggins
02-04-2004, 05:23 PM
its ok. it was too complicated, so i've turned to a php solution.

thanks guys.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum