Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:39:23+00:00 2026-05-14T20:39:23+00:00

This question will undoubtedly be difficult to answer and ask in a way that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:39:23+00:00Added an answer on May 14, 2026 at 8:39 pm

    If I understand what you’re trying to do correctly:

    1. Define types
    First, you need to define what type of field requires what kinds of rules, e.g.:

    $type['FirstName']['requirement'] = 'required';
    $type['FirstName']['error_message'] = 'The First Name field is required.';
    // just random example: $type['MobilePhone']['requirement'] = 'optional'; $type['MobilePhone']['error_message'] = 'Only enter digits.';

    2. Go through each posted value
    Second, check for each posted value what type of field it is. Now a lot of stuff might be included in the $_POST array, and you only need to check certain fields. It might make it a lot easier to use names like checkthis;1234;FirstName for your input fields.

    foreach ($_POST as $key => $value) {
      // split the key, so you know what's what:
      $data = explode(';',$key);
    // now $data[0] tells you what kind of field it is // $data[1] is the ID of the enrollee (your Num1, Num2) // $data[2] is the type of field
    // only do checks for posted fields that start with "checkthis" if ($data[0]=='checkthis') { // now you can fill the $rule array: $rule[] = $type[$data[2]]['requirement'] . ',' . $key . ',' . $type[$data[2]]['error_message']; } }


    This way it doesn’t matter what you include in your form. As long as you’ve defined the type of field, a rule will be added to the rule array if it’s included in the $_POST values, and your validation script will do the rest.


    Edit
    If you structure your input fields like this:

    
    <input type="text" name="F1Firstname" value="" />
    <input type="text" name="F2Firstname" value="" />
    etc..

    ..and you submit the form, the $_POST array will e.g. look like this:

    //print_r($_POST) gives:
    
    Array
    (
        [F1FirstName] => John
        [F2FirstName] => Jane
    )

    ..the easiest thing to do is to loop through each of those with foreach:

    foreach ($_POST as $key => $value) {
      // first $key will be "F1FirstName"
      // first $value will be "John"
    
      // second $key will be "F2FirstName"
      // second $value will be "Jane"
    
      // etc
    }

    Now, in that $_POST will also be other types of fields, like e.g. F1MobilePhone or whatever, that require different validations and different messages. So for each of those fields, you need to find out what type it is, to determine what kind of message to enter into your validation function. One option would be (this goes within the foreach statement):

    if (strstr($key,'FirstName')) {
      // add a validation rule for the FirstName type
    }
    if (strstr($key,'Somethingelse')) {
      // etc
    }

    I don’t know how your validation script works, but I’m guessing you’re doing something like this:

    // import the validation library
    require("validation.php");
    
    $rules = array(); // stores the validation rules
    
    // All Enrollee Rules
    $rules[] = "required,Num1FirstName,The First Name field is required.";
    
    // Call on the validation function
    $result = validate($rules);

    Then you’ll probably receive back whether, in this case, “Num1FirstName” was valid or not. Since you use an array $rules, the validator can probably handle multiple lines at once. Simply add more values to the array, but don’t add another variable. If you do this:

    $rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

    ..the validator will think that the "Num2FirstName" is the error message, since that's the 3rd value you enter, and it won't know what to do with the actual message which is now 4th value. Try this instead:

    $rules[] = "required,Num1FirstName,The First Name field is required.";
    $rules[] = "required,Num2FirstName,The First Name field is required.";

    So bringing that all together gives you this:

    foreach ($_POST as $key => $value) {
      // first $key will be "F1FirstName"
      // first $value will be "John"
    
      // second $key will be "F2FirstName"
      // second $value will be "Jane"
    
      if (strstr($key,'FirstName')) {
        $rules[] = "required,$key,The First Name field is required.";
      }
      if (strstr($key,'Somethingelse')) {
        $rules[] = "required,$key,Some other message for this type of field.";
      }
    }
    
    // call on your validate function and feed it the $rules array you've created

    Adding JavaScript to (pre-)validate is a whole different story. In the end you still need to validate it in PHP because, like you said, the JS check can easily be bypassed. So I guess fixing this first might be best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.