Page 285 - Beginning PHP 5.3
P. 285

Chapter 9: Handling HTML Forms with PHP
                         Next the script defines some helper functions. validateField() is used within the form to display a
                         red error box around a form field label if the required field hasn’t been filled in. It’s passed a field
                         name, and a list of all the required fields that weren’t filled in. If the field name is within the list, it
                         displays the markup for the error box:

                             function validateField( $fieldName, $missingFields ) {
                               if ( in_array( $fieldName, $missingFields ) ) {
                                 echo “ class=”error”;’;
                               }
                             }



                            setValue()  is used to prefill the text input fields and text area field in the form. It expects to be passed a
                         field name. It then looks up the field name in the   $_POST  superglobal array and, if found, it outputs the
                         field ’ s value:

                             function setValue( $fieldName ) {
                               if ( isset( $_POST[$fieldName] ) ) {
                                 echo $_POST[$fieldName];
                               }

                             }
                            setChecked()  is used to preselect checkboxes and radio buttons by inserting a  checked  attribute into
                          the element tag. Similarly,   setSelected()  is used to preselect an option in a  select  list via the
                            selected  attribute. Both functions look for the supplied field name in the  $_POST  array and, if the field
                         is found and its value matches the supplied field value, the control is preselected:

                             function setChecked( $fieldName, $fieldValue ) {
                               if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
                                 echo ‘ checked=”checked”’;
                               }
                             }

                             function setSelected( $fieldName, $fieldValue ) {
                               if ( isset( $_POST[$fieldName] ) and $_POST[$fieldName] == $fieldValue ) {
                                 echo ‘ selected=”selected”’;
                               }
                             }

                           Next comes the form handling function,  processForm() . This sets up an array of required field names,
                          and also initializes an array to hold the required fields that weren ’ t filled in:
                             function processForm() {
                               $requiredFields = array( “firstName”, “lastName”, “password1”, “password2”,
                             “gender” );
                               $missingFields = array();








                                                                                                         247





                                                                                                      9/21/09   7:23:43 PM
          c09.indd   247
          c09.indd   247                                                                              9/21/09   7:23:43 PM
   280   281   282   283   284   285   286   287   288   289   290