CGI - Common Gateway Interface

CGI - Common Gateway Interface, Part 2: Making a CGI Input Form

To make a CGI input form, you need two files to handle it: First, the HTML form, which is basically a normal HTML document with some special HTML codes in it. The other file you need is the CGI script to process the input from that form.

Here's a sample HTML document outlining the basic things you need to have to make a form:

<FORM ACTION="/cgi-bin/">

<input size=50 name=valname>
<input type=submit value=Click here to submit>


The FORM tag gets the actual form code started. The ACTION parameter, as you might have guessed, is supposed to point to the CGI script to handle the form's input.

The INPUT tag makes a type of input... Since the first one doesn't specify a type, it uses a text-entry field by default. The size specifies the width, and the name specifies the name of the value to be passed on to the CGI script.

The second INPUT tag makes a SUBMIT type of input, which is actually just a button which you click to submit the form information to the CGI script. The VALUE parameter just specifies what that button should say on it.

So, the HTML code we see above just makes a text-entry box and a submit button. Whatever you type in the box will be passed on to the CGI script! Fairly simple, isn't it? Now the next question, obviously, is how do you write the actual CGI script?

The input data from the form is sent to the CGI script in an environment variable called QUERY_STRING. To demonstrate how to use it, here's a quick CGI script in Perl which simply prints out the data sent to it:


print "Content-Type: text/html\n\n";

$input= $ENV{'QUERY_STRING'} ;

print "<HTML><BODY>", $input, "</BODY></HTML>";

Now, note that the URL syntax for entering things into that CGI script is:


(Where blahblah is the stuff you typed into the input text box.)

As you can see, the parameter is signified by a question mark, the parameter name, an equals sign, and then its value. The question mark is only used for the first variable; Every subsequent one uses an ampersand. So, for example:

Note that because all the values for the form are sent to the CGI script as one long string, you have to parse that string before you can do anything useful with this input. This is a fairly simple process with Perl. Here's a small routine which will split the query string into two Perl variables, one called $varname and one called $varvalue. As you might guess, these variables will hold the form's variable name and that variable's value, respectively. Then this routine prints only the value, leaving off the name:


print "Content-Type: text/html\n\n";

($varname, $varvalue) = split('=', $ENV{'QUERY_STRING'}, 2) ;

print $varvalue;

Of course, if you want to set $varname to $varvalue, just do this:

$varname = $varvalue;

The following are the valid types of inputs in HTML forms:
"text" (text entry field; this is the default)
"password" (just like "text", except what you type isn't actually shown in the box; instead, asterisks are used as placeholders for the characters)
"checkbox" (a single-toggle check box)
"radio" (a button which can have the same name as other radio buttons, only one of which can be selected at once)
"submit" (a button that submits the form to the server)
"reset" (a button that resets the input fiels in the form to their default values)
"image" (used for graphical submit buttons)
"file" (allows the user to attach a file to the form; often used by web-based e-mail systems to send file attachments with e-mail)
"hidden" (does not actually show up on the HTML form; used to set variables in the CGI script which the user is not meant to change)

(NOTE: Drop-down boxes are made with a <SELECT> tag, not an <INPUT> tag.)

Well, that's about all for now... At least this should get you started on writing CGI scripts. The rest is mostly refining the input and processing it, which requires knowing Perl (assuming you use Perl for the script, which you probably will).

Back to the main page