I’m trying to validate some form data before adding it to a database however I need to validate that the option was an option that is in the form, not just one that was added with something like Firebug.
I tried using if and comparing the posted values like this: (there is another 27 options, and 3 more option fields)
if($data['selection'] == 'some_option' || $data['selection'] == 'some_option2') {
However it makes extremely long lines of code and its difficult to manage. So I thought about making an array that stores the possible options, then when its submitted, check if the selected option matches one of the entries in the array.
One of my ideas was to do a loop and check if it’s equal:
for($i = 1; $i < sizeof($options[$currentselection]); ++$i) {
if($option[$i] == $data[$currentselection]) {
return true;
} else {
return false;
}
}
However I’d have to do one of these for each of the selection fields (which I’d rather not do). So is there a way of cleaning it up and having it all in one? Also, I’m using codeigniter.
A simple way is to use the
in_arrayfunctionOr you can do this: