When validating form input, create a unique key and value pair with an associated error message. If, after evaluating all the input, the error array isn't empty, iterate over the array and print the errors.
1
<?php
2
/*
3
Checking Form Data & Error Messages
4
http://www.beliefmedia.com/code/php-snippets/soft-break
5
*/
6
7
8
/* Create array to store error values */
9
$arrErrors = array();
10
11
/* Error checking */
12
if (ereg('[^a-zA-Z]', $fn)) $arrErrors['fne'] = 'First name can contain letters only.';
13
if ($fnl < 2) ) $arrErrors['fnl'] = 'First name must be more than two characters.';
14
15
/* If errors... */
16
17
18
/* Add error text to an error string of text */
19
$strError = '<strong>Please correct the following errors:</strong><ul>';
20
21
/* Get each error and add it to the error string as a list item */
22
foreach ($arrErrors as $error) {
23
$strError .= '<li>' . $error . '</li>';
24
}
25
26
$strError .= '</ul>';
27
28
/* Output error to screen */
29
echo '<ul>' . $strError . '</ul>;
30
}