PHP Form Validation
- by JM4
This question will undoubtedly be difficult to answer and ask in a way that makes sense but I'll try my best:
I have a form which uses PHP to display certain sections of the form such as:
<?php if ($_SESSION['EnrType'] == "Individual") { display only form information for individual enrollment } ?>
and
<?php if ($_SESSION['Num_Enrs'] > 6) { display only form information for 7 total members enrollment } ?>
In each form piece, unique information is collected about each enrollee but the basic criteria for each enrollee is the same, i.e. All enrollee's must use have a value in the FirstName field. Each field is named according to the enrollee number, i.e. Num1FirstName; Num2FirstName.
I have a PHP validation script which is absolutely fantastic and am not looking to change it but the issue I am running into is duplication of the script in order to validate ALL fields in one swoop.
On submission, all POSTED items are run through my validation script and based on the rules set return an error if they do not equal true.
Sample code:
if (isset($_POST['submit']))
{
// import the validation library
require("validation.php");
$rules = array(); // stores the validation rules
//All Enrollee Rules
$rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";
The script above does the following, $rules[] ="REQUIREMENT,fieldname,error message"
where requirement gives criteria (in this case, simply that a value is passed), fieldname is the name of the field being validated, and error message returns the error used.
My Goal is to use the same formula above and have $rules[] run through ALL firstnames and return the error posted ONLY if they exist (i.e. dont check for member #7's first name if it doesnt exist on the screen).
If I simply put a comma between the 'fieldnames' this only checks for the first, then second, and so on so this wont work.
Any ideas?