Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

29 CGI data transmission


..A browser can send data to the server.  Most commonly, the data is simply the address
of a URL for the server to return .  However, in the case of a form, the browser can
send the contents of the form elements to the server.  JavaScript can even be used to
send data to the server without using a form.

There are two possible formats for send such data - get or post.  In both cases, the
browser send the URL of a program that will be used to process the data, followed
by the data itself.  The two formats, get and post, differ in how the data is sent.

GET method  < form method = "get" action = "myapp.exe" >
--------------------
Here are examples of the strings sent from the browser to the server using the get format. In
this example, the words 'Gary' and 'Beene' are sent, as is the string "100 MyLane" .

http : //www.garybeene.com/cgi-bin/myapp.exe?firstname=Gary&secondname=Beene&address=100+MyLane
or
http : //www.garybeene.com/cgi-bin/myapp.exe?firstname=Gary&secondname=Beene&address=100%20MyLane

Everything up to the question mark is the full path name of the application to be run by the server.

Everything after the question mark is called the query string or the search string.  It consists of name = value
pairs, separated by question marks.  See below for special formatting rules.

The server runs the application.  It also passes the application the search string for processing.

The application returns results to the web server in the form of a text stream - usually a complete
HTML document, although it could be simply a image to return to the browser.

The GET method is usually limited by the browser to a few thousand characters.

The query string is formatted in a special way :
- Special characters, including spaces, must be replaced by their hexadecimal characters.
- Spaces can also be replaced by the plus sign '+'

The CGI application that receives the data has to be able to interpret the query string, decoding the
special characters as well as extracting all of the name pairs


POST method  < form method = "post" action = "myapp.exe" >
----------------------
The post method sends a special header after the URL, tellling the server how many bytes of data will
follow.  The data transmission then follows the header.  No limit applies to the number of bytes that may
be transmitted this way.