Case Study: Adding Sessions to the Winestore

In this section we use sessions to improve the user interaction with the client entry <form> developed in Chapter 6. The improvements focus on the interaction when the <form> is submitted and fields don't validate. We modify the scripts to:

  • Display error messages on the client entry <form>

  • Use session variables to pass back the submitted fields to the <form> generation script, saving the user rekeying all the data to correct the errors

Improving the Client Entry <form>

The client entry <form>, generated by the script shown in Example 6-7, collects client fields to either create a new client or edit the details of an existing client. The script shown in Example 6-8 performs the server-side validation of the client <form> data, and updates or inserts a row in the customer table if there are no errors.

If the validation fails, the script shown in Example 6-8 generates a page to display the errors to the user, and the user then follows a hypertext link back to the client entry <form> to reenter the fields. The solution provided by Example 6-7 and Example 6-8 suffers three problems:

  • The user is forced to reenter the entire client entry <form> from scratch when an error is encountered during validation

  • The errors that are encountered during validation are displayed by Example 6-8 and not the entry <form> where they would be useful

  • The error page generated by Example 6-8 isn't safe from the reload problem described in Chapter 6

In this section we develop the scripts to make use of session variables to solve these problems. Rather than displaying the error messages on a page generated by the validation script, we make the necessary changes to display the errors in red above the appropriate fields on the client entry <form>, as shown in Figure 8-2.

Figure 8-2. Client entry <form> showing error messages placed above the appropriate fields

Both the script that generates the client entry <form> and the script that validates the data need to be modified to use sessions and the session variables. Because the validation script processes the fields collected in the client <form> and generates any associated errors, we look at the changes required for that script first.

by BrainBellupdated