|
|||||||||
|
Processing input from fill-out formsFill-out forms are set up within HTML documents as described in Chapter , 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 ), 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 ), 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:
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: Processing image map Up: Writing CGI scripts Previous: The cgiutils program
Spinning the Web by Andrew Ford |
||||||
Copyright © 1996-2002 Ford & Mason Ltd |