Ford & Mason Ltd
HOME
ABOUT
CONTACT
RESOURCES
ADVERTISING



donations support the development of
cronolog.org
and
refcards.com

next up previous contents index
Next: Processing image map Up: Writing CGI scripts Previous: The cgiutils program


Processing input from fill-out forms

Fill-out forms are set up within HTML documents as described in Chapter gif, but are processed on the server system. When a user submits a form, the data is encoded and passed to the server, which invokes a CGI script to process it. The data may be sent by one of two HTTP methods: GET or POST. How the data is encoded depends on the HTTP method specified in the HTML form.

If the form specifies METHOD="GET", the data will be url-encoded in the URL as a query string (see Section gif), and the server will place this string in the QUERY_STRING environment variable.

If the form specifies METHOD="POST", the data will be passed to the server in the body of the request and then from the server to the CGI script in the script's standard input stream. The length of the data is placed in the CONTENT_LENGTH environment variable and the CONTENT_TYPE environment variable will be set to application/x-www-form-encoded. The script should not try to read more data from the standard input stream than the number of bytes specified by the content length variable, as the stream will be connected to the server's output stream and, on some systems, if the server does not close its end of the stream, the script will hang waiting for more data to appear.

Fill-out forms consist of a set of fields. When the form is submitted by the user, the browser sends the name and value of each field, for all the fields that contain data (some fields may be empty). The field names and values are url-encoded (see Section gif), and separated by the ampersand character (&). Field values are sent as name/value pairs, for example the string:

    field1=1&field2=three+separate+words&field3=%7e

contains three fields: field1, field2 and field3, with values: the numeric value 1, a string containing the text `three separate words', and a single encoded character (~).

The process of decoding forms data consists of:

  1. Splitting the string into fields on the ampersand character.

  2. Splitting each field into a name and value on the equals character.

  3. Decoding each name and value by replacing plus signs with spaces, and then replacing characters that were encoded as a per cent sign followed by a hexadecimal number by the actual characters.

Form data may contain duplicate field names. This is permitted for multiple value selection fields: each selected value is sent as a name/value pair. Image button fields are sent as two separate name/value pairs, named field-name.x and field-name.y.

Programs should not assume that the encoding of data received will be valid or that the field names and values received will be those that are expected.


next up previous contents index
Next: Processing image map Up: Writing CGI scripts Previous: The cgiutils program

[ITCP]Spinning the Web by Andrew Ford
© 1995 International Thomson Publishing
© 2002 Andrew Ford and Ford & Mason Ltd
Note: this HTML document was generated in December 1994 directly from the LaTeX source files using LaTeX2HTML. It was formatted into our standard page layout using the Template Toolkit. The document is mainly of historical interest as obviously many of the sites mentioned have long since disappeared.

 
Copyright © 1996-2002 Ford & Mason Ltd