|
|
Handling Forms in Perlby Brent MichalskiJune 26, 1998 When you learn how to handle HTML forms, you open yourself up to a whole new world of possibilities. Forms, in my opinion, are the most powerful feature of the Web. Forms are so powerful because with them you can create new programs and/or "front-ends" to existing programs that use the Web browser as the user-interface. Using the Web browser as the user-interface is a great idea because people are familiar with their browsers and don't require additional user-interface training for each new program. Sure, they may need training on the new program, but you have eliminated the need for a big portion of the training and you start with a foundation they are comfortable with. This week's article covers the basics of form processing using the cgi-lib.pl library file. Next week we'll show you how to use the CGI.pm module. The Libraries and ModulesI chose to start out with the cgi-lib.pl library file. I know many of the Perl guru's out there will give me a hard time for using this library, but let me list the reasons I still like it.
The FormSince this is a Perl article and not a tutorial on HTML forms, I won't be covering how I created the form. If you need some reference materials on forms, I suggest a trip over to Yahoo! as a starting point.Here is a simple form. If you are unsure of the HTML that goes into a form, you can view the source code to see how I created it. Submitting the DataWhen you hit the form's submit button, you set off a series of events.
On to the ProgramNow we move to a simple program that will read the data submitted from our form, and create a (hopefully) humorous paragraph about Perl. While this may not sound very exciting, it should open up your mind to some new possibilities. This is just the beginning of a whole new way to use the Web.I have numbered the lines of code, the line numbers are not part of the program. If you want to see the program without the line numbers, you can view the source code here. The line numbers simply make it easier for me to talk about the program. Also, I only number the lines of code; I do not number comments or blank lines. 1: #!/usr/bin/perl ######################## # A simple CGI program # # to handle form input # # By: Brent Michalski # ######################## 2: require "cgi-lib.pl" || die "Could not find cgi-lib.pl!"; 3: &ReadParse; 4: $adjective1=$in{'adj_1'}; 5: $adjective2=$in{'adj_2'}; 6: $adjective3=$in{'adj_3'}; 7: $verb1=$in{'verb_1'}; 8: $verb2=$in{'verb_2'}; 9: $verb3=$in{'verb_3'}; 10: $fluid=$in{'fluid'}; 11: $result=$in{'num_1'} * $in{'num_2'}; 12: print &PrintHeader; 13: print<<HTML; 14: <HTML><HEAD><TITLE>The Perl 15: Language</TITLE></HEAD> 16: <BODY BGCOLOR="#FFFFFF"> 17: <FONT FACE=ARIAL> 18: <CENTER><FONT SIZE=6>The Perl Language</FONT></CENTER> 19: <HR WIDTH=80%> 20: <P> 21: <FONT SIZE=4> 22: <BLOCKQUOTE> 23: Perl is a very $adjective1 programming 24: language. It has many features that let 25: you do things like $verb1, and $verb2 26: with regular expressions. The best 27: thing about Perl is that you can 28: multiply $in{'num_1'} and $in{'num_2'} 29: and get: $result. Nobody 30: <I>really</I> understands 31: Perl but some really $adjective2 people 32: think they do. They post to USENET a 33: lot. They should all be $verb3 and 34: dumped into a $adjective3 vat full of 35: $fluid. 36: </BLOCKQUOTE> 37: <P> 38: <HR WIDTH=80%> 39: </BODY></HTML> 40: HTML Line-by-line ExplanationLine 1: Tells the program where to find Perl on the Web server. This line will vary depending on where Perl is installed on your server, so you may need to make some changes. On a UNIX server, this line is required. If you are running this program on an NT server, the line is not required but won't hurt anything if included.Line 2: Brings in the cgi-lib.pl file so that we can access its subroutines. This is much like C's include statements. The || die "Could not find cgi-lib.pl! causes the program to stop and print out the line of text if cgi-lib.pl could not be found. Line 3: Call to a subroutine in cgi-lib.pl. This subroutine handles the decoding of the data that was passed from the form. All values which are decoded are stored in an associative array named in. Lines 4-10: Assign the values passed from the Web form, to variables. Assigning to variables is not required but it makes handling the data much easier when you get into the program. If you don't do this, to access a variable you would have to do something like: instead of:print "The value is: $in{'value1'}\n"; To me, the second example is much easier to read.print "The value is: $value1\n"; Remember: When you read the values passed from the form, they are case-sensitive and must match exactly what you named them in the HTML form. Line 11: Takes the values in num_1 and num_2 we passed from the form, multiplies them together and stores the result in the variable called $result. Line 12: Calls a subroutine in cgi-lib.pl that prints the standard header for CGI programs. In all CGI programs, the first print statement must tell the Web server what type of data it is sending. This is also called the MIME type. For most CGI programs, it looks like this: Notice that there are two \n's at the end; these are required elements. The ReadParse subroutine is simply a subroutine that prints this line out. I recommend choosing whichever way you find easiest to remember and sticking to it. Personally, I print it myself like in the above statement.print "Content-type: text/html\n\n"; Line 13: Begins a printing block of HTML. This is called the here-document syntax. Whatever you specify after the << is the terminating string. The terminating string can be anything you want; in this case, I chose HTML. Anything between this "top" line and the terminating line will be printed out, just as it would if you had used print statements and quotes. As you can see, printing out large blocks of text this way can save a lot of typing. If I hadn't done it this way, each line would have to start with print ", and end with ";. Lines 14-39: These lines are just HTML. Notice that I have included variable names in the text. The variable names are converted to the values stored in each variable when the program runs. Notice on line 31 that I put data in directly from the in associative array and didn't store it in a variable first. This is OK to do, but it can lead to programs that are harder to read. Line 40: This is the terminating line. It tells the here-document to quit printing. It must be all the way to the left side, and it must exactly match the terminating-string we used in line 13. We could have more program below line 40, but stopped because we were done with this program. Wrapping It UpWell, we have covered enough ground for this week. We covered a little about MIME types, here-documents, and reading the values from the HTML form that called the program.Forms, combined with CGI can be used for many things. Comment forms, online database front-ends, and email interfaces are just a few. Next week, I'll talk about using the CGI.pm module with forms. In the meantime, keep playing with forms and try to think of some new, creative uses for them.
Handling Forms in Perl:
Source Code
Next: Handling Forms in Perl with CGI.pm
|
|
|
Web Techniques and Web Design and Development copyright © 1995-99 Miller Freeman, Inc. ALL RIGHTS RESERVED |