How to make validation of data from an array? Here is an example of how I am doing:

The form:

<form class="" action="index.php" method="post"> <input type="text" name="code" placeholder="Enter nubmer"> <input type="submit" value="code" class="button"> </form> 

PHP code:

 $_POST = $code; $code = array("CodeOne", "CodeTwo", "CodeThree", "CodeFour"); if (in_array('', $code)) { echo "Nice! Product is verification!"; } else (in_array('', $code)) { echo "Oh, no! Product is not verification!"; } 
  • if you want to assign the $code array, all that came from the post, you need to do $ code = $ _POST, and not vice versa. and what do you want to do with this? $code = array("CodeOne", "CodeTwo", "CodeThree", "CodeFour"); ? From the example, you can get only $_POST['code'] - ddeadlink
  • I want to do this: for example, I have “Machine”, “Bicycle”, “Helicopter” in the array, and if I enter in the form “Machine” - I get true, or rather the correct answer. - WhiteCoder
  • So can, it is more correct to give the chance of a choice through? - splash58 pm
  • @dirk Please post your comment as a response. - Nicolas Chabanovsky ♦

1 answer 1

In your case, you can simply remove empty values ​​from an array like so:

 $code = array_diff($_POST['code'], ['']);