Interactivity on the Web

Where do I get the form data from?

There are two methods which can be used to access your forms. These methods are GET and POST. Depending on which method you used, you will receive the encoded results of the form in a different way.


But what does it all mean? How do I decode the form data?

When you write a form, each of your input items has a NAME tag. When the user places data in these items in the form, that information is encoded into the form data. The value each of the input items is given by the user is called the value.

Form data is a stream of name=value pairs separated by the & character. Each name=value pair is URL-encoded, i.e. spaces are changed into plusses and some characters are encoded into hexadecimal. Keywords are separated with the + plus.

Because others have been presented with this problem as well, there are already a number of programs which will do this decoding for you. The basic procedure is to split the data by the ampersands. Then, for each name=value pair you get for this, you should URL decode the name, and then the value, and then do what you like with them.


GET

Using the GET method assumes there is a CGI script and the browser is sending a request to "GET" the information or action from the script. For example, the page counter, the system variables, date and time, and the calendar are "GETs". In each case the HTML text has a line that requests something from the server.

For example, the date and time routine is invoked using the statement

    <!--#exec cgi="/cgi-bin/dater.cgi"-->

The routine to be invoked is named "dater.cgi", and resides in the cgi-bin directory of the server. The actual location and name is very server dependent.

When the routine is invoked, the server executes it. Depending on what the routine does determines what happens. In this case, the routine is a UNIX shell script that calls the system time and date routine then sends that information back to the browser in the appropriate format - meaning HTML statements.

To give you an example, here's the script for dater.cgi:

      #!/bin/sh
      echo ""
      date +"%a %b %e, %Y"
      echo ""

All this routine does is invoke the UNIX date routine and returns the information in a specific format. The echo command means to display on the screen EXACTLY what is contained on the rest of the line. In other words, display a blank line. The requirement of this approach is the need to work with UNIX shell commands. Many providers do not allow users to develop CGI routines. Most provide a small group of "canned" CGI routines, such as the collection at BPL.

To invoke the system environment routine vars.cgi, a PERL script is used:

#!/usr/bin/perl

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

$ss = $ENV{"SERVER_SOFTWARE"};
$sn = $ENV{"SERVER_NAME"};
$su = $ENV{"SERVER_URL"};
$gi = $ENV{"GATEWAY_INTERFACE"};
$rm = $ENV{"REQUEST_METHOD"};
$sp = $ENV{"SERVER_PORT"};
$rh = $ENV{"REMOTE_HOST"};
$ra = $ENV{"REMOTE_ADDR"};
$ru = $ENV{"REMOTE_USER"};
$ua = $ENV{"HTTP_USER_AGENT"};
$hf = $ENV{"HTTP_FROM"};
$sc = $ENV{"SERVER_PROTOCOL"};
$path = $ENV{"PATH_INFO"};
$qu = $ENV{"QUERY_STRING"};
$sm = $ENV{"SCRIPT_NAME"};
$ht = $ENV{"HTTPS"};
$hk = $ENV{"HTTPS_KEYSIZE"};

print "<br>Server Software: ".$ss."\n";
print "<br>Server Name    : ".$sn."\n";
print "<br>Server URL     : ".$su."\n";
print "<br>Gateway I/F    : ".$gi."\n";
print "<br>Request Method : ".$rm."\n";
print "<br>Server Port    : ".$sp."\n";
print "<br>Remote Host    : ".$rh."\n";
print "<br>Remote Address : ".$ra."\n";
print "<br>Remote User    : ".$ru."\n";
print "<br>User Agent     : ".$ua."\n";
print "<br>Email from     : ".$hf."\n";
print "<br>Server protocol: ".$sc."\n";
print "<br>Path Info      : ".$path."\n";
print "<br>Query String   : ".$qu."\n";
print "<br>Script Name    : ".$sm."\n";
print "<br>Security?      : ".$ht."\n";
print "<br>Keysize        : ".$hk."\n";
1;

Note that the line print "Content-type: text/html\n\n"; is required to tell the web browser that the information returned is plain text. The rest of the statements are PERL commands that get the specific information from the $ENV or system environment table, and formats it (using the print statements) into HTML statements.

Again, the need to know not only the UNIX system but also the PERL language.


POST

The POST command, on the other hand, allows the browser to send information to the server, and let the server do something with it. Anything that can be done with a GET can also be done with a POST. A GET, however does NOT allow the browser to send information or make a request to the server.

The POST is useful for


Using Forms

There are two parts to a form - designing the physical layout and handling the interaction. Most forms require a CGI script (server-side include) but simple mail can also be useful and doesn't need special CGI. For the form to do something, there must be a CGI script that is invoked when the SUBMIT button is selected.

Any HTML editor (HotDog, PageMill, etc) support the basic design of the layout - the fields, actions, defaults and so on.

FORM components - The Basics

See the HTML reference card for details or pp 44-49 in Aronson.
Return to Teknikos Home Page