Chapter 5. Writing Form-Based Applications

only for RuBoard - do not distribute or recompile

Chapter 5. Writing Form-Based Applications

graphics/chic01.gif

Chapter 4, Generating and Processing Forms, covered the general techniques involved in creating forms. In this chapter, we ll use those techniques to write several types of form-based applications, and in Chapter 6, Automating the Form-Handling Process, we ll discuss how you can use information in your database to help you handle forms automatically.

Web programming includes such a diverse range of applications that we can t hope to cover more than a fraction of the possibilities. Nevertheless, there are several recurring issues, and the applications in this chapter illustrate a number of useful techniques that you should be able to apply to many of your own projects. Therefore, although useful in their own right, these applications are not just ends in themselves they serve an illustrative purpose as well. The projects we ll tackle, and some of the techniques they involve, are as follows:

  • A product-registration script that enables customers to register purchases online by visiting your Web site rather than by mailing in a paper form. It shows how to generate and validate a form on the basis of specifications stored in a data structure.

  • A guestbook. I guess every Web programming book has to have a guestbook, so this one does, too. However, our version serves only as a means by which to demonstrate how to incorporate email capabilities into your scripts. The guestbook itself uses email to help keep you apprised of new entries, and we ll cover other ways you can use mail capabilities in your applications. The section also discusses how to set up jobs that execute according to predetermined schedule.

  • A giveaway contest application that enables visitors to your site to submit contest entries. This section covers some basic fraud-detection techniques to help combat ballot-box stuffing, selection of random entries to choose contest winners, and entry summary and expiration methods.

  • A simple poll. We ll develop a script that uses a form to present candidates users can vote for, and that displays a results page showing the current vote totals. The application uses MySQL to count the votes, and the results page uses the current totals in the database so that the results shown are always up to date.

  • Image-storage and image-display scripts. Images are an integral part of many Web applications, which necessitates a method for getting them to your server host and accessing them from within your scripts. To provide support for image use, we ll illustrate how to load images into MySQL two ways: over the Web using a form containing a file-upload field, and from the command line. In addition, this section shows how to display images in Web pages by pulling them from your database.

  • An application that enables you to construct an electronic greeting card interactively and notify the recipient that it s waiting. The card is stored as a database record so that it can be retrieved and displayed later for the recipient. The application also notifies you when the recipient views the card. This application incorporates image-display capabilities and shows how to implement multiple-stage record construction, how to trigger a notification when a record s status changes, and how to handle removal of expired cards.

The applications in this chapter have very different purposes, but share certain common characteristics. Generally, you ll find that form-based programs involve the following steps, although the steps vary in complexity from application to application, depending on your goals and requirements:

  • Generate a form to solicit the information you want to collect from the user. Some forms are relatively trivial: Our polling application presents a form containing nothing more than a set of radio buttons listing the candidates and a Submit button. The user clicks one time to pick a candidate, a second time to submit the vote, and that s it. Other forms are more extensive: The product-registration application has many fields because we need to gather information about both the product being registered and the user who s registering it.

  • Validate the form s contents when the user fills it in and submits it. Form validation can be minimal or quite extensive. You may have fields that are required but were left blank by the user, or fields that must contain a certain kind of information but were filled in incorrectly. In such cases, the user must submit additional or revised information. You ll find it necessary to inform the user that the form cannot be processed, as well as what should be done to correct any problems. We ll demonstrate several feedback techniques over the course of the chapter.

  • Store the form submission. Some applications store form information in a file or mail it somewhere for further processing. This being a book on MySQL, we will of course focus on using a database as the primary storage mechanism for each application.

  • Generally, you provide some kind of feedback to the user after a form submission has been received and processed. This can be quite simple, such as a thank you message expressing appreciation for the user s participation in a poll or survey, or a brief acknowledgement that the submission was received. Or you may redisplay the information to provide confirmation to the user that it was received properly.

A small reminder before you proceed: You ll find the source code for the scripts developed here in the webdb software distribution that accompanies this book. You can get it at the following Web site:

http://www.kitebird.com/mysql-perl/

You may find it useful to install each script and try it out first before you read the section that describes how it works.

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