How to build a FORM object in your HTML page
Okay, this is a two part step: The first part is a very quick step which tells the FORM where to send the information once the user presses the SUBMIT button that we talked about in Preface to CGI. The "tag" in HTML that tells the page where to send the information is called the ACTION tag, and is an attritude tag just inside the FORM itself. Example of starting form that you can copy:
ACTION
FORM
<FORM METHOD=POST ACTION="http://www.stimulus.com/cgi-bin/lesson5.pl">
Once you've created the FORMs ACTION tag, you can then start laying out the FORM FIELDS. As we showed you in our Preface to CGI lesson, the best method for laying out fields is to use TABLE objects. This is to ensure that the fields are spaced evenly. The other professional trick is to make sure that the FIELDS have the correct SIZE and MAX tags. This will make sure that someone doesn't type too many characters inside a particular FIELD. Since most people use standard CUT and PASTE methods of creating HTML forms, its not too uncommon to see amateur forms that look like this:
Even though this is a quick method of making forms, it forever demonstrates a complete lack of professional industrial strength data gathering. You wouldn't create a window like this to gather information inside a database application. The main question is: What do you do with all the extra data? Maybe its important information that the user entered, not knowing what you were doing using a 35 character long State FIELD.
So the golden rules about FORMs, are to make sure the SIZE is set appropriately, and that the MAX limits the number of characters that can be typed into in a single FIELD. Here is a better example of a form that collects the correct amount of data:
You'll notice that the above form has different sizes for the various fields, while it also limits the number of characters that the user can type in to match the number of characters that have been used in the database where we plan to store the information. Granted, this form will only work in the United States, because of the 2 character STATE field, but we thought this was a good way to make a point.
After you have laid out your FIELDs, it is time to add the SUBMIT button. The code for this looks like:
<INPUT TYPE="submit" VALUE="Submit Form">
The SUBMIT button as we mentioned in CGI Preface, actually tells the internet browser to send the information in the form fields to the destination in the SUBMIT part of the FORM tag. In our case, the data will be sent to the lesson5.pl PERL script sitting on the STIMULUS server.
Great, I know this part, where does it go?
You guys ask the best questions! Alright, this is where we enter the world of CGI. Remember, CGI is merely a language type, it isn't a language in itself. For this lesson we are going to use the most common CGI language in the world, Perl.
The way this works, is there is a Perl compiler (an application that can run Perl scripts) on the STIMULUS.COM server. When we send this information to a particular Perl script, that "compiler" compiles the scripts instructions, processes the information that was sent to it, and does any number of things in return. In our case, we are going to collect the information from the fields, and send an HTML page back to the user as a response.
One of the best ways to think about writing Perl scripts is to break things down into tasks. When processing incoming information, we:
Here is a typical Perl script in the smallest form:
#!/usr/bin/perl # # This electronic Perl script was written by # #[Your name here] #©Copyright 1998-06, [Your company]. All Rights Reserved. require "cgi-lib.pl"; &ReadParse(*in); $name = $in{"name"}; print "Content-type: text/html\n\n"; print "<HTML>"; print "<HEAD><TITLE>CGI Process: $name\n</TITLE></HEAD>"; print "<BODY>"; if($name eq "") { print "<H1>Invalid Name. Please Try Again.</H1></BODY></HTML>"; exit (0); } print "The name is <b>", $name, "</b>"; print "</BODY></HTML>";
That's it. That is the entire script that will take a single field called "name" and check to see if it has a value. If it does have a value it then sends back an HTML document to the browser to display that name. Now we realize that Perl doesn't look familiar, but it will soon.
Now you try it!
The following field information will post to the lesson5.pl CGI, and return results. The script that is executing, is EXACTLY the script above.
Name:
Now lets review the Perl script ONE STEP AT A TIME
Lets take the first line:
#!/usr/bin/perl
This is an important line that tells the server where to find the Perl "compiler." The compiler is the piece of software that reads the script, and compiles it into machine language so the computer can understand what to do with it. Once its compiled, its ready to run.
# # This electronic Perl script was written by # #[Your name here] #©Copyright 1998-06, [Your company]. All Rights Reserved.
Now the rest of the lines that start with "#" signs, are lines of comments. Only the first line with the "#" is used by the server to do anything. All other lines that begin with "#" are ignored by the compiler when its trying to figure out what to do with the script. It is always a good idea to comment your code. You and others will forget what the heck you were doing, even with a well written script, because it will probably run for several months (and maybe even years) before its ever changed. Bottom-line, COMMENT YOUR CODE!!!!
#
Getting a little help from the internet itself
Moving right along, in order to make things easier for us to get values from the form itself, we're using a file that was created by someone else. This file is called "cgi-lib.pl" and it contains a library of procedures that we can use in our code. The most widely used by us, is the &ReadParse() routine. NOTE: You can find this file at http://www.bio.cam.ac.uk/cgi-lib/.
&ReadParse()
require "cgi-lib.pl";
Reading in all the values
After we've required the cgi-lib.pl file, we can now take advantage of its power. We do this by calling the &ReadParse() routine which will (okay bare with me) read all the values into an array called "in." We could have called it anything, buy we chose "in."
in
&ReadParse(*in);
in[0]
in[1]
in[2]
in['name']
in['address']
in['state']
Getting individual field values from the "in" array
As we said above, its time to get the value from the "name" field. This is done as follows:
$name = $in{"name"};
$
Setting up the return page
Now we need to grasp a new concept. This concept relates to the way that a Perl script communicates back with the browser. When you send a submission to a CGI script (regardless of language), the server creates what is known as a "pipe" from the script to the browser. This "pipe" is used to send information back to the browser. In Perl, the old "print" command from basic programming days, is used to send information directly back through the pipe that was initially created. In our case, the pipe talks directly back to the browser. Since our goal is to send an HTML page back to the browser in response to the submission, we have to set up the "header" information for the HTML page. This is done with the following code:
print
print "Content-type: text/html\n\n"; print "<HTML>"; print "<HEAD><TITLE>CGI Process: $name\n</TITLE></HEAD>"; print "<BODY>";
Content-type: text/html\n\n
\n
The rest of the print commands should be understood as normal HTML page header information that you learned from the previous HTML lessons.
Testing to see if we got a valid name
The next logical step we must preform is a test to see if the user gave us a name to return to them. If they didn't, we want to tell them so. This is accomplished by using an "if" statement. This is very similar to most computer languages. We use the "eq" argument to see if the variable "$name" that we set up in a previous line equals something. The easiest way to do this is to test to see if it equals nothing, then warn against that eventuality. NOTE: The "eq" is used for comparing variables that contain "text" data. The "=" sign is used for comparing "numeric" data. Otherwise, an "=" acts as a verb that will set the variable to a value, e.g., x = 3, which makes x = 3.
if
eq
$name
=
If we didn't get a value, this code will execute completely:
if($name eq "") { print "<H1>Invalid Name. Please Try Again.</H1></BODY></HTML>"; exit (0); }
exit (0);
If they did give us a value
If there is a value in the $name variable, then the above argument will fail and the code will continue to execute. At this point, we send back a page that repeats the name that was provided.
print "The name is <b>", $name, "</b>"; print "</BODY></HTML>";
"The name is <b> $name </b>"
Recommended Books:
©Copyright 1996-06, TWIN MONOLITH. All Rights reserved.