| home / programming / php_forms2 / 1 | [previous][next] |
|
|

When validating a form, most of the time the emphasis is on mandatory fields.You can check if they contain values in two ways:
|
It is very important that you combine both techniques.
You always have to check for a field’s existence
using isset() to avoid error messages when trying to
access array values that do not exist. But you always
have to check whether there is something within the
field apart from whitespace because text fields are
always submitted. In addition, when empty, isset() always returns true independent of the field’s value.


When it comes to validating a selection list, the approach depends on the type of list:
|
Options with empty strings as values come into play if the list contains functionless dummy entries that have captions such as “Please choose from list.” These list options must not receive a value other than " ", so that the validation algorithm can distinguish these entries from reasonable ones.
The listing at the beginning of this phrase validates a single selection list—again including PHP code to prefill the list.

With multiple lists, a bit more work is required, as shown in the following code. If the form has been submitted, the array of selected list elements is searched. If one element that is nonempty is found, the process is complete and the user can be congratulated for the successful completion of the form. Otherwise, the form is displayed again.

| home / programming / php_forms2 / 1 | [previous][next] |
Created: March 27, 2003
Revised: January 23, 2006
URL: http://webreference.com/programming/php_forms2/1