An Introduction to CGI - The Common Gateway Interface
by Jay Eckles
Name/Value Pairs
When a user calls on your gateway program by submitting a form, then
input comes in the query string in a name/value pair format. What this
means is that the query string contains the name of a field in the form
followed by an equals sign (=) followed by the value that was
entered into that form of the field, followed by subsequent name/value
pairs. Multiple name/value pairs are usually separated by an ampersand
(&) when they are submitted via a web browser, but you should
also check to see if a semicolon (;) is used to separate the
pairs. If a you put a link to your program in an HTML document, then you
may prefer to separate the name/value pairs using the semicolon rather
than the ampersand because the ampersand has a special meaning in HTML.
Generally this does not cause display problems in most browsers, but it
at least makes validating your HTML documents a pain.
Here's an example of a decoded query string that contains name/value pairs separated by an ampersand:
FirstName=Jay&LastName=Eckles&Age=20&Height=8'11"
In order for this information to be useful, you need to break the
query string up into tokens, using the & or
; as your delimiter. Standard library functions exist in C
and Perl for performing this task, as well in other languages I'm
sure.
One common way of dealing with name/value pairs and making them
useful and easy to use in your program is to build a hash-table or
associative array out of these pairs. The name part of the pair is used
as the key, and the value is used as the information stored under that
key. This way you can easily retrieve the information associated with a
form field by simply calling the hash-table function or associative
array with the name of the field as the key.
Most gateway programs are designed to handle all of their input in
this name/value format. This way, if the program is to be initiated by a
form or via a link or some other method, there is a conventional,
intelligent way to format the input to be sent to it. For example, if
you were going to call a gateway program from a link and send the input
to it via this name/value format, the anchor tag would look something
like this:
<a href="cgiprog.cgi?name1=value1&name2=value2">Click Here</a>
In a case like this, the name and the value of the name/value pairs need to be URL-encoded in the link.
[Contents] [Next] [Previous]
If you have any questions or would like to contact me for any reason, please email me at j.eckles@computer.org.
|