I currently use the following for validating data entered: function validate_fields($s) { $forbidden = array('\r', '\n', 'test1', 'test2', 'test3', 'test4'); foreach ($forbidden as $f) if (strpos($s, $f) !== false) return false; return true; } elseif (!validate_fields($_POST['field1'])) { echo "Error....<br>";} PHP: So that if test1 or test2 is entered in field1 it will report an error, however I have 75 fields numbered 1-75, so is there a quicker and easier way to validate and show the error than having to repeat the elseif code for each of the 75 fields with the error message? Thanks Ian
foreach $_POST as $field { if (!validate_fields($field)) { echo "Error....<br>"; } PHP: $_POST is just an array like anything else...
Tops, but doesn't that still mean I have to do this for each field...? foreach $_POST as $field { if (!validate_fields($field)) { echo "Error....<br>"; } foreach $_POST as $field1 { if (!validate_fields($field1)) { echo "Error....<br>"; } foreach $_POST as $field2 { if (!validate_fields($field2)) { echo "Error....<br>"; } PHP: and so on...? Ian
Or if they're just numbered field1-75, it almost as easy to do just those fields instead of everything all the POST fields.. for ( $x=1 ; $x<=75 ; $x++ ) { if ( ! validate_fields($_POST['field'.$x]) ) { echo "Error....<br>"; } } PHP: EDIT: No, the foreach loop will loop through every element in the post array (each time assigning the value to the variable $field). Regardless of how the fields have been named, everything submitted via POST will be checked.
No because they are $_POST['field1'], $_POST['field2'] etc. so by foreach-ing through $_POST you catch them all no matter whether there are 5 or 5000. You'll probably want to slightly change my code because now you potentially get 75 error messages, it could break after one for example. Rodney's suggestion works too but hard codes the number of fields. Which you could get round with sizeof() but anyway, there's always more than one way.