CGI Forms   «Prev  Next»

Lesson 3 Passing data to the server
ObjectiveLearn how data is passed from the form to the server

Passing Data to the Server in Perl

When a Perl script is used to handle data from a web form, the process of passing data from the client to the web server involves the Common Gateway Interface (CGI), a standard protocol for interfacing external applications with web servers. Here's how the process typically works:
  1. Form Creation and Data Input:The client-side form is created using HTML. This form includes various input elements like text fields, radio buttons, checkboxes, etc., where users can enter or select data. The form's `action` attribute is set to the URL of the CGI script (written in Perl) that will process the data, and the `method` attribute is set to either `GET` or `POST`, determining how data will be sent to the server.
  2. Data Submission:When the user submits the form (usually by clicking a submit button), the browser packages the data entered into the form fields. The packaging format depends on the method specified in the form:
    • With `GET`, data is appended to the URL as a query string, following a question mark (`?`). Each key-value pair is separated by an ampersand (`&`), and keys are separated from values by equals signs (`=`).
    • With `POST`, data is included in the HTTP request body, not in the URL, allowing for larger amounts of data to be transferred without being visible in the URL.
  3. Data Reception and Parsing by CGI Script:The web server receives the HTTP request and, recognizing the URL as a CGI script, executes the script. The Perl CGI script then accesses the form data:
    • For `GET` requests, data is accessed via the environment variable `QUERY_STRING`.
    • For `POST` requests, the script reads the data from the standard input (STDIN).
  4. Using the CGI Module:Perl scripts often use the `CGI` module to simplify data handling. This module provides functions to parse form data, making it easier to access form field values. For instance, the `param` function can be used to retrieve the value of a specific form field.
  5. Processing and Response:The Perl script processes the received data according to its logic (such as storing it in a database, performing calculations, etc.). It then typically generates an HTML page or another form of HTTP response, which is sent back to the client’s browser.
  6. Client-Side Display:The client's browser receives the response from the server and renders it, completing the cycle of client-server interaction. This response might be a confirmation message, the results of a query, or a new web page.

It's important to note that while CGI scripts can be written in Perl, they can also be written in other programming languages. Perl, however, has been traditionally popular for CGI due to its strong text-processing capabilities and the ease of handling HTTP requests and responses with the CGI module.


Now that you know how to make the form, let's take a close look at how the data gets delivered to the server and then to the CGI program.

Client to Server

After the user fills out the form and clicks the Submit button, the client (usually a Web browser) delivers the form to the server along with the HTTP request. There are two basic methods that the client can use to make the request: GET and POST. The CGI program must be ready to accept the appropriate method. In the case of the form in the previous lesson, the method was GET, so the client would send a request to the server that looks something like this (I had to break the HTTP request string up over a couple of lines to fit on your screen, but typically it would be one continuous string. The same applies to the Accept string.):

GET /test.cgi?name=Fooplah+Klinczaghi&age=Over+the+hill&listening=on
&formis=Mauve&formalso=Plethoric&info=NRP+1-56205-571-2 HTTP/1.0
Referer: http://luna/formtest.html
Connection: Keep-Alive
User-Agent: Mozilla/3.01 (Win95; I)
Host: luna
Accept: image/gif, image/x-xbitmap, image/jpeg, 
        image/pjpeg, */*

  1. The first line is the HTTP request, and the lines after it are all HTTP header lines.
  2. Each of the header lines is passed to the CGI program, as we will see later in the lesson.
  3. For the GET method (which is what this example uses), the form data is passed along with the URL after a ? separator.
  4. For the POST method, the form data is passed in the body of the request, after the header lines.
  5. A blank line separates the headers from the body of the message (just like in an email message).
  6. The server then passes the query on to the CGI program along with other information from the browser.
The next lesson discusses that process.
Now, what do you think the overall strengths and weaknesses of forms are?