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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:16:17+00:00 2026-05-29T11:16:17+00:00

In order to prevent bad data collection, I was thinking about a way to

  • 0

In order to prevent bad data collection, I was thinking about a way to prevent unwanted values from being accepted by my PHP code.

I’m using the same list to populate the values that I validate against. This would apply to drop downs, radios, and checkboxes.

Granted, this is a very simplified example, but I just wanted to run this idea past some other programmers to see if I’m way off base here.

// get form value
$name = $_POST['color'];

// form list values
$arr =  array("blue", "red", "green");

// build options
for($i = 0, $size = sizeof($arr); $i < $size; ++$i)
{
echo '<option value="' . $arr[$i] . '">' . $arr[$i] . '</option>';
}

// verify value is valid
if (in_array($name, $arr)) {
    // valid value
}
  • 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-29T11:16:18+00:00Added an answer on May 29, 2026 at 11:16 am

    I believe this is a good way to do it, though I’d probably do all the logic of checking right at the top of the script, ie:

    // set the error flag
    $error_flag = false;
    
    // form list values
    $arr =  array("blue", "red", "green");
    
    if(isset($_POST)){
        // get form value
        $name = $_POST['color'];
    
        // verify value is valid
        if (in_array($name, $arr)) {
            // valid value
        } else {
            // error - you selected an invalid value!
            $error_flag = true;
        }
    
        if($error_flag == false){
            // process data and save to database, etc etc
        }
    }
    
    if($error_flag == true){
        // display the error
    }
    
    // build options
    for($i = 0, $size = sizeof($arr); $i < $size; ++$i){
        echo '<option value="' . $arr[$i] . '">' . $arr[$i] . '</option>';
    }
    

    I’ve added in an error flag so you can also notify end users of what they’ve done wrong and allow them to complete the form again. Of course you can expand it so the correct fields ‘remember’ the data that was already sent before hand, whilst indicating which fields were bad.

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

Sidebar

Related Questions

In order to prevent somebody from grabbing my data easily, I cache data from
In order to prevent a SQLite in-memory database from being cleaned up, one must
I was wondering if they was a way to prevent some commands from being
It is said that in order to prevent from SQL injection one should filter
In order to prevent command windows from popping up during installation I am using
I want to block hardware back button in android ,in order to prevent from
I'm new to Java so, I think in order to prevent this error from
I have a table which stores 'links' between 2 people. In order prevent further
how can I catch windows taskkill command in order to prevent it?
i am using php and running sql queries on a mysql server. in order

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.