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 1049093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:33:40+00:00 2026-05-16T16:33:40+00:00

I have a form being validated in the following manner: //Clear all variables $formCheck

  • 0

I have a form being validated in the following manner:

//Clear all variables
$formCheck = '';
$rep = '';
$name = '';
$department = '';
$location = '';
$email = '';
$phone = '';
$type = '';
$drink = '';
$notes = '';
$lastVisited = '';
$nextVisit = '';

$clean_formCheck = '';
$clean_rep = '';
$clean_name = '';
$clean_department = '';
$clean_location = '';
$clean_email = '';
$clean_phone = '';
$clean_type = '';
$clean_drink = '';
$clean_notes = '';
$clean_lastVisited = '';
$clean_nextVisit = '';

function validateRep($rep){
...some code...
}

$formCheck = $_POST["formCheck"];
$rep = $_POST["rep"];
$name = $_POST["name"];
$department = $_POST["department"];
$location = $_POST["location"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$type = $_POST["type"];
$drink = $_POST["drink"];
$notes = $_POST["notes"];
$lastVisited = $_POST["lastVisited"];
$nextVisit = $_POST["nextVisit"];

if (validateRep($rep)){
    $clean_rep = $rep;
}else{
    echo "Invalid Rep";
    exit();
}
//.....and so on......

I was wondering if it would be more efficient / cleaner to validate using an an array instead of individual variable? If so, how would I go about that, and how would I write the different validation functions all in one (eg. right now I have a separate function to validate each field), would it be possible with a loop through the array? i was experimenting and so far this is what I have:

$unclean['formCheck'] = $_POST["formCheck"];
$unclean['rep'] = $_POST["rep"];
$unclean['name'] = $_POST["name"];
$unclean['department'] = $_POST["department"];
$unclean['location'] = $_POST["location"];
$unclean['email'] = $_POST["email"];
$unclean['phone'] = $_POST["phone"];
$unclean['type'] = $_POST["type"];
$unclean['drink'] = $_POST["drink"];
$unclean['notes'] = $_POST["notes"];
$unclean['lastVisited'] = $_POST["lastVisited"];
$unclean['nextVisit'] = $_POST["nextVisit"];


$clean = array(
        'rep', 'name', 'department', 'location', 'email', 'phone', 'type', 'drink', 'lastVisited', 'nextVisit',
);

but I’m not sure how to proceed from here.

  • 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-16T16:33:40+00:00Added an answer on May 16, 2026 at 4:33 pm

    I would use something along these lines… Just coded this very quickly, basically you create validation functions that match the post fields and return true or false if the validation passed. e.g. validate_department, validate_type, validate_drink, etc. Will work if your post data doesn’t have any strange characters in (which so far it doesn’t)

    $post_fields = array('rep',
                          'name',
                          'department',
                          'location',
                          'email',
                          'phone',
                          'type',
                          'drink',
                          'lastVisited',
                          'nextVisit'
                   );
    
    $validate = new Validate();
    
    foreach ($post_fields as $post_var)
    {
      if (isset($_POST[$post_var]))
       {
          $validate->validate_data($post_var, $_POST[$post_var]);
       }
    }
    
    if ($validate->all_fields_valid() === true)
    {
      echo 'congrats, all validation passed!';
    }
    else
    {
       echo 'oh no! error in validation process. please see below errors: <p>' .
             $validate->get_error_msg() . '</p>';
    }
    

    And the validate class… Use $errorMsg to see the error messages should you run into any issues

    class Validate
    {
      var $valid = 0,
          $error = 0,
          $errorMsg = '';
    
      function validate_data($var, $data)
      {
        if (method_exists($this, 'validate_'.$var))
        {
          if (call_user_func(array($this, 'validate_'.$var), $data) === true)
          {
            $this->valid++;
          }
          else
          {
            $this->throwError('validation for: "'.$var.'" was not considered valid');
          }
        }
        else
        {
          $this->throwError('validation function for: "'.$var.'" does not exist');
        }
      }
    
      function throwError($msg = '')
      {
        if ($msg) $this->errorMsg .= $msg . '<br/>';
        $this->error++;
      }
    
      function all_fields_valid()
      {
        if (!$this->error) return true;
        return false;
      }
    
    /***********************************************
    *************************************************
     Custom validation functions go below here
       Function format: validate_<postFieldName>
       Returns: true or false if the data passed is valid or not
    *************************************************
    *************************************************/
    
      function validate_type($type)
      {
        if (is_numeric($type)) return true;
        return false;
      }
    
      function validate_lastVisited($data)
      {
    
    
      }
    
      //etc...............
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Form being launched from another form on a different thread. Most
I have a file upload form that is being posted back to a servlet
I have an asp.net url path which is being generated in a web form,
I have a form like this: <form name=mine> <input type=text name=one> <input type=text name=two>
I have the following code, repeated on each Form , as part of the
I have a GridViewColumn created with the following XAML: <GridViewColumn Header=Validated > <GridViewColumn.CellTemplate> <DataTemplate>
I have form area in my view. If I click button A , I
I have form that displays several keywords (standard set of choice lists that changes
I have a form in C# that has a button that, when clicked, I
I have subclassed Form to include some extra functionality, which boils down to a

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.