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
/* Create array to store error values */
8
$arrErrors = array();
9
10
/* Error checking */
11
if (ereg('[^a-zA-Z]', $fn)) $arrErrors['fne'] = 'First name can contain letters only.';
12
if ($fnl < 2) ) $arrErrors['fnl'] = 'First name must be more than two characters.';
13
14
/* If errors... */
15
16
17
/* Add error text to an error string of text */
18
$strError = '<strong>Please correct the following errors:</strong>
19
<ul>';
20
21
/* Get each error and add it to the error string as a list item */
22
foreach ($arrErrors as $error) {
23
$strError .= '
24
<li>' . $error . '</li>
25
26
';
27
}
28
29
$strError .= '</ul>
30
31
';
32
33
/* Output error to screen */
34
echo '
35
<ul>' . $strError . '</ul>
36
37
;
38
}