MockY
Programmer
- Jul 7, 2006
- 94
I am creating a form that currently work just fine but I do want to add some validation to reinforce required fields. I am using a javascript that works just like I want it too, but only for fields. Throughout the form, I am using a fair amount of check boxes, and in this thread I am cutting the form down to bare minimum so it is easier for me to show what I am trying to accomplish, but it is fully functional.
The problem that I am having is that I use arrays as names for the checkboxes. This is because I use PHP loops for display of the content of the boxes and I can't therefore address the checkboxes individually. First and foremost, below is the form (just a snippet but in working condition)
And here below is the javascript that I am using to validate the required fields:
This form requires the user to fill in information in 3 of the fields. So far so good. Using this javascript and the form together, it works just fine. However, now comes my issue.
Depending on what box you check (Proof or Additional), further requirements are needed.
In this case, If you check the Additional checkbox, one or more of the job type boxes are required to be checked as well. But If you check the Proof box, no further validation is required at all and the user can send the form.
How do I go about to do this?
For the one who wonder, an example PHP loop that I use in the target file looks like this:
The problem that I am having is that I use arrays as names for the checkboxes. This is because I use PHP loops for display of the content of the boxes and I can't therefore address the checkboxes individually. First and foremost, below is the form (just a snippet but in working condition)
Code:
<form method="post" name="form" action="formmail.php" >
<div>Information</div>
<table>
<tr>
<td>Name:</td>
<td><input name="holdername" type="text" size="26" /></td>
<td>Policy#:</td>
<td><input name="holderpolicy" type="text" size="26" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input name="holderphone" type="text" size="26" /></td>
<td>Email:</td>
<td><input name="holderemail" type="text" size="26" /></td>
</tr>
</table>
<table>
<tr>
<td><input type="checkbox" name="requirements[]" value="Proof" /></td>
<td>Proof</td>
</tr>
<tr>
<td><input type="checkbox" name="requirements[]" value="Additional" /></td>
<td>Additional</td>
</tr>
</table>
<table>
<tr>
<td colspan="6">What is the job type?</td>
</tr>
<tr>
<td><input type="checkbox" name="jobtype[]" value="New Stuff" /></td>
<td>New Stuff</td>
<td><input type="checkbox" name="jobtype[]" value="Remodel" /></td>
<td>Remodel</td>
<td><input type="checkbox" name="jobtype[]" value="Old Stuff" /></td>
<td>Old Stuff</td>
</tr>
</table>
<input type="submit" value="Send" name="submit" id="formbutton" onclick="return verify()" />
</form>
And here below is the javascript that I am using to validate the required fields:
Code:
<Script language="JavaScript">
function verify() {
var themessage = "You are required to complete the following fields: \n\n";
if (document.form.holdername.value=="") {
themessage = themessage + " Holder Name\n";
}
if (document.form.holderphone.value=="") {
themessage = themessage + "Holder Phone Number\n";
}
if (document.form.holderpolicy.value=="") {
themessage = themessage + "Holder Policy #\n";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: \n\n") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
</script>
This form requires the user to fill in information in 3 of the fields. So far so good. Using this javascript and the form together, it works just fine. However, now comes my issue.
Depending on what box you check (Proof or Additional), further requirements are needed.
In this case, If you check the Additional checkbox, one or more of the job type boxes are required to be checked as well. But If you check the Proof box, no further validation is required at all and the user can send the form.
How do I go about to do this?
For the one who wonder, an example PHP loop that I use in the target file looks like this:
Code:
if(is_array($_POST['requirements']))
foreach($_POST['requirements'] as $requirementschecked)
{
$requirementsprint .= "$requirementschecked";
}