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

  • Home
  • SEARCH
  • 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 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can only access files that belong to your application… May 16, 2026 at 3:23 pm
  • Editorial Team
    Editorial Team added an answer This should do it: string style = "ErrorLabel"; Style styItem… May 16, 2026 at 3:23 pm
  • Editorial Team
    Editorial Team added an answer Tools -> Options -> Content -> Languages. You will see… May 16, 2026 at 3:23 pm

Trending Tags

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

Top Members

Related Questions

Im trying my first form validation with PHP. I need some guidance with the
I am trying to implement form validation using cakephp models. Here are my code
I am trying to do some very simple form validation checking for null or
I'm trying to create a form validation. I added @user.errors.add_to_base TEST to my controller,
I am trying to set up validation on a simple contact form that is
im trying to make a registration form with ajax validation check. validation check is
I'm trying to use MVC2 client-side validation in a partial view that is rendered
I'm trying to convert an image into an audio signal in MATLAB by treating
I just wanted a more elegant automated solution to my form validation. On PHP.net
I'm trying to add a new method to jQuery validation plugin with the codes

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.