Page 1 of 1

Form validation will not work

Posted: Fri Aug 29, 2008 5:26 pm
by dirtyvegas14
I'm trying to modify this php form and I'm told there is a problem with the onblur code. Can anyone help me fix this? Thanks a ton!

<?php
$id = $_POST['id'];

if ($id == "1"){



$myemailaddr = "14@cristan.com"; // Put your email address Here.
$mysubject = "2009 Entertainment Book Fundraiser Form"; // Put the subject of the email you will recieve Here.


$contactname = $_POST['contactname'];
$contactname = $_POST['age'];
$contactname = $_POST['grade'];
$contactname = $_POST['school'];
$contactname = $_POST['parentname'];
$phone1 = $_POST['phoneareacode'];
$phone2 = $_POST['phone'];
$email = $_POST['email'];

if (!$contactname == "" && !$age == "" && !$grade == "" && !$school == "" && !$parentname == "" && !$phone1 == "" && !$phone2 == "" && !$email == ""){

$rcpt = $myemailaddr;
$subject = $mysubject;
$body = "New Submission\n-------------------------------\n\nName: $contactname\nAge: $age\nGrade: $grade\nSchool: $school\nParentname: $parentname\nPhone: $phone1 - $phone2\nEmail: $email";
$headers = 'From: 14@cristan.com' . "\r\n" . 'Reply-To: 14@cristan.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($email, "2009 Entertainment Book Fundraiser Form", "This letter is to confirm your 2009 Entertainment Book Fundraiser submission was received.", $headers);
mail($rcpt, $subject, $body, $headers);
echo "<span class='redhighlight'>Thank you! Your information has been received. Check your email for a confirmation.</span>";
} else {
echo "<span class='redhighlight'>Please fill in all required fields.</span>";
}
}
?>
<style type="text/css" media="screen">
@import url("../css/layout.css");
</style>


<script 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[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.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);
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>



<form action="" method="post">
<table width="100%">

<tr>
<td class="td_underline"><span class="redhighlight">*</span>Contact Name </td>
<td colspan="2" class="td_underline"><input name="contactname" type="text" onblur="MM_validateForm('contactname','','R','age','','R','grade','','R','school','','R','parentname','','R','phone','','R','email','','R');return document.MM_returnValue" value="<?php echo $contactname;?>" size="25" /></td>
</tr>
<tr>
<td class="td_underline"><span class="redhighlight">*</span>Age</td>
<td colspan="2" class="td_underline"><input type="text" name="age" size="25" value="<?php echo $age;?>" /></td>
</tr>
<tr>
<td class="td_underline"><span class="redhighlight">*</span>Grade</td>
<td colspan="2" class="td_underline"><input type="text" name="grade" size="25" value="<?php echo $grade;?>" /></td>
</tr>
<tr>
<td class="td_underline"><span class="redhighlight">*</span>School</td>
<td colspan="2" class="td_underline"><input type="text" name="school" size="25" value="<?php echo $school;?>" /></td>
</tr>
<tr>
<td class="td_underline"><span class="redhighlight">*</span>Parent's Names</td>
<td colspan="2" class="td_underline"><input type="text" name="parentname" size="25" value="<?php echo $parentname;?>" /></td>
</tr>
<tr>
<td class="td_underline"><span class="redhighlight">*</span>Daytime Phone #</td>
<td colspan="2" class="td_underline"><input name="phoneareacode" type="text" id="phoneareacode" value="<?php echo $phone1;?>" size="3" />
<input type="text" name="phone" size="20" value="<?php echo $phone2;?>" /></td>
</tr>

<tr>
<td class="td_underline"><span class="redhighlight">*</span>E-mail Address</td>
<td colspan="2" class="td_underline"><input type="text" name="email" size="25" value="<?php echo $email;?>" /></td>
</tr>
<tr>
<td class="td_underline"><input name="submit" type="submit" id="submit" value="Submit Form" />
<input type="hidden" name="id" value="1" /></td>
<td colspan="2" class="td_underline"><input name="reset" type="reset" id="reset" value="Reset" /></td>
</tr>
</table>
</form>

Re: Form validation will not work

Posted: Sun Aug 31, 2008 4:23 pm
by JAB Creations
You need to validate per step. That's just a nutty number of parameters to send to a function!

Each text input element should have it's own onblur JavaScript event attribute. Create smaller functions so when they tab out of the field you validate it then. I'd recommend changing to the border color in example to green or red based on validation minimally to give the user a visual queue.

Then when you submit the form run each of the functions again, either one by one or called by a parent function (such as how multiple window.onload functions are called).