Search code examples
phpsanitizationwhitelist

How to apply a whitelist to an input that has a drop down menu


How can I use sanitisation on drop down menu's?

I know how to sanitise using normal user input something like this:

$variable_name = preg_replace( "/[^a-zA-Z0-9_]/", "", $_POST['key'] );

But how can I produce something like this for a drop down - say a list of fruit?


Solution

  • The best thing to do is to put the possible choices in an array, iterate over it when generating the form element, and use in_array() to validate it.