CGI (Common Gateway Interface) scripts provide interactive communication with the user. Typical usage of these scripts is for form processing.
Consider the following form that asks for your name and your favorite color. Enter your name an color and click on "Submit" button. The computer will print a greeting and change the background color to your favorite one.
How does it work? First, you need to learn several new tags for creating input fields. They will be explained in this section. Second, when you click the Submit button, the information that you have entered (name and color) is passing to a server (remote computer) over the Internet. The server executes a special program (specified in the form, you will learn it later). This program returns a new HTML script to the browser, containing your name and the corresponding background color.
The program responsible for the user interface is called CGI script. In this case it is written in Perl language, and a slightly simplified version of this program is presented below. Perl is not a part of this course, but the simple example below might shed some light on what is going on.
| Perl script | Comments |
|---|---|
#!/usr/bin/perl
use CGI ':standard';
$n = param('name');
$c = param('color');
print header,
"<html> <body bgcolor='$c'>",
"<h2>Hello, $n! How are you doing?</h2>",
"<p>",
"<form>",
"<input type='button' ",
" value='Back to the form' ",
" onclick='history.back()'>",
"</form>",
"</body> </html>";
|
location of Perl interpreter use the Perl package CGI fetch name in the variable n fetch color in the variable c print a specific CGI header HTML returned to the browser put your name to the greeting small vertical space starting a form to get back programming "Go back" button text on the button a JavaScript instruction closing the form Closing the HTML script |
As you can see from this CGI script, it constructs HTML script to be returned back to the browser. The returned HTML contains some form related tags and attributes to be specified in this section.
Another (much more complicated) CGI script involved in this course is called any time you enter your name and password to get to the course Forum, Gradebook, Guest Book, and Settings. This script, for example, has an access to the database with the class scores, selects related to you records, and converts them "on the fly" upon request into HTML to display them on your screen.