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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:19:34+00:00 2026-05-13T07:19:34+00:00

I am trying to do form validation by putting the post values into variables

  • 0

I am trying to do form validation by putting the post values into variables and those variables into an array and then cycling through them and outputting error messages for fields that are not filled in.
I am having two issues. Firstly, the if statement is running for all the values even if the field is empty or is == to ‘undefined’ and secondly i don’t know how to print out the actual name of a variable instead of the variable value. For example

$variable = 'hello';

print_x($variable)//prints 'variable' instead of 'hello'

I have tried two methods which are shown below.

   $error_message = "The following fields must be filled in:<br />";
        $fields_to_validate_arr = array($category,$producer,$product_name,$image_name,$description,$stock_quantity,$min_sale);
        foreach($fields_to_validate_arr as $v){
            if(empty($v) || $v = 'undefined'){//using variable bariables
                //increment the error message with a custom error message. 
                $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables
            }
        }

And a different method where i use variable variables

$error_message = "The following fields must be filled in:<br />";
    $fields_to_validate_arr = array('category','producer','product_name','image_name','description','stock_quantity','min_sale');
    foreach($fields_to_validate_arr as $v){
        if(empty($$v) || $$v = 'undefined'){//using variable bariables
            //increment the error message with a custom error message. 
            $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";//no need to use variable variables
        }
    }

The variables are assigned further up in my code like

$category = myescape_function($_POST['category']);

Thanks

  • 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-13T07:19:34+00:00Added an answer on May 13, 2026 at 7:19 am

    There’s no need to create your own input variable array, since you already have $_POST:

    $_POST = array_map('myescape_function', $_POST);
    foreach($fields_to_validate_arr as $v){
        if(empty($_POST[$v]) || $_POST[$v] == 'undefined'){
           //increment the error message with a custom error message. 
           $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";
        }
    }
    

    Since the values aren’t stored in separate variables, the problem of printing a variable’s name rather than it’s value goes away.

    If you want to get really fancy, you can add support for custom validators:

    function inputExists($name, &$source) {
        return !empty($source[$name]) && 'undefined' != $source[$name];
    }
    function inputIsNumeric($name, &$source) {
        return inputExists($name, $source) && is_numeric($source[$name]);
    }
    // checks for U.S. phone numbers only
    function inputIsPhone($name, &$source) {
        if (inputExists($name, $source)) {
            // strip whatever non-numeric 
            $value = preg_replace('/[-.,() \t]+/', '', $source[$name]);
            return preg_match('^(1?[2-9]\d{2})?[2-9]\d{6}$', $value);
        }
        return False;
    }
    function inputMatchesRE($name, &$source, $RE) {
        return inputExists($name, $source) && preg_match($RE, $source[$name]);
    }
    function nameAndValidator($name, $validator) {
        if (function_exists($validator)) {
            return array('name' => $name, 'validator' => $validator, 'data' => '');
        } elseif (is_numeric($name)) {
            // if index is numeric, assume $validator actually holds the name
            return array('name' => $validator, 'validator' => 'inputExists', 'data' => '');
        } else {
            return array('name' => $name, 'validator' => 'inputMatchesRE', 'data' => $validator);
        }
    }
    
    $fields_to_validate_arr = array('name', 'street' => '/^\d+ +[a-z ]+(ave|st|wy|way|ln|lp|blvd)$/i', 'age'=> 'inputIsNumeric', 'phone' => 'inputIsPhone');
    
    $_POST = array_map('myescape_function', $_POST);
    
    foreach($fields_to_validate_arr as $name => $validator){
        list($name, $validator, $data) = nameAndValidator($name, $validator);
        if(! call_user_func($validator, $name, $_POST, $data)){
           //increment the error message with a custom error message. 
           $error_message .= "->" . ucwords(str_replace('_',' ',$v)) . "<br />";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 422k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You declare mySortedLists to have length table.Rows.Count * table.Columns.Count, but… May 15, 2026 at 11:27 am
  • Editorial Team
    Editorial Team added an answer Got it!. This can be done by calling the getSkinUrl()… May 15, 2026 at 11:27 am
  • Editorial Team
    Editorial Team added an answer You may need to do some checking on sizes, but… May 15, 2026 at 11:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.