Displaying previously entered values

As Example 6-7 already sets the value attribute of the <input> elements from the array $formVars, there are no changes needed to display previously entered values; Example 6-7 uses $formVars when displaying the current values of clients from the customer table. By setting $formVars as a session variable, Example 6-7 displays the values passed back from the validation script with each <input> field.

Displaying error messages

Changes are required to display the errors that are saved in the session variable $errors in the validation script. We have added the function fieldError( ) to help display the error messages above the <input> fields. The function takes two parameters: $errors, which is the associative array of error messages, and $fieldName, which is the index into the array.

function fieldError($fieldName, $errors)
  if (isset($errors[$fieldName]))
    "<font color=RED>$errors[$fieldName]</font><br>";

This function tests if the indexed error message exists and, if so, echoes an appropriately formatted error message. When each <input> element is displayed, a call is made to the fieldError( ) function, as shown for the firstName and surname fields:

  <td><font color="red">First name:</font></td>
  <td><? echo fieldError("firstName", $errors); ?>
      <input type="text" name="firstName"
         value="<? echo $formVars["firstName"]; ?>"
  <td><font color="red">Surname:</font></td>
  <td><? echo fieldError("surname", $errors); ?>
      <input type="text" name="surname"
         value="<? echo $formVars["surname"]; ?>"

Figure 8-2 shows the final results: a client entry <form> with error messages placed over the corresponding fields.

by BrainBellupdated