Chapter 6. Automating the Form-Handling Process

only for RuBoard - do not distribute or recompile

Chapter 6. Automating the Form-Handling Process

graphics/chic01.gif

Four phases of form processing were identified in Chapter 5, Writing Form-Based Applications : generating the form for display to the user, validating the contents of the form when the user submits it, storing the contents of the form in the database, and presenting confirmation to the user. For the form-based applications that we ve built thus far, MySQL has come into play primarily during the third phase (as a means of storing form responses). The role of MySQL in each application has thus been to serve as a repository a box in which to save information collected from people who visit our site.

In this chapter, we ll examine the potential for using MySQL not just as a storage bin for information obtained through form submissions, but also as a more active participant in the form-handling process. In particular, we ll focus on pulling information from the database and using it to generate forms and evaluate the responses that users submit. In other words, this chapter explores some of the ways to use your database to help you process fields or entire forms on an automatic basis. This is possible because MySQL can provide you with several useful sources of information:

  • You can use table structure metadata.

    When you re using a form containing fields that correspond to the columns in a database table, it s often possible to use information about the table s structure (also known as metadata) to help you create elements of the form. For example, you can ask MySQL for a description of a column s characteristics so that you can figure out how long to make a text-input field that corresponds to the column. You can determine the list of legal values for an ENUM or SET column, and then use the list to construct a form field such as a set of radio buttons for an ENUM or a set of check boxes for a SET. You can also use information about a table s structure to validate values submitted for form elements. For example, if a column has an integer type, the value of the corresponding field must also be an integer.

  • You can generate field elements from the results of queries.

    Although an application may present a form designed to gather information for entry into a particular table, forms often can be improved by referring to the contents of other tables. Suppose you have an entry form for creating personnel table records, where each record includes a department code. If you have a department table that lists the names and codes of each department, you can use its contents to make the personnel form more understandable: Have your application display department names (which are more meaningful than codes to people), and then map the name selected by the user to the code when adding the record to the personnel table. Other kinds of information are so commonly used that you may as well store them in the database instead of repeating the information in multiple scripts. A State Name field in a form that gathers address information typically has about 50 legal values. If you put the states into your database, any script can run a query to look them up and create a pop-up or scrolling list.

  • You can set up tables designed specifically to store form descriptions.

    If you have occasion to present several forms that are similar to each other, you may be able to store descriptions for them in the database. Then you can write a routine that queries the tables and generates the entire form, and others that validate form responses and store their contents, all on a completely automated basis. An example that we ll explore in this chapter is construction and validation of forms for online polls.

By automating certain aspects of the form-generation and form-processing cycle using the information that MySQL can provide to you from your database, you reduce the amount of application-specific code in your scripts. In effect, your database becomes a development tool for writing code that is more general and reusable. In the process of discussing the techniques described in this chapter, we ll develop code for a couple of Perl modules that live in the WebDB namespace and that can be reused for multiple applications.

only for RuBoard - do not distribute or recompile


MySQL and Perl for the Web
MySQL and Perl for the Web
ISBN: 0735710546
EAN: 2147483647
Year: 2005
Pages: 77
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net