Like HowStuffWorks on Facebook!

How CGI Scripting Works

        Tech | Programming

Putting It All Together

Let's say that you would like to create a simple questionnaire for one of your Web pages. For example, you would like to ask for the reader's name, sex, age and comment, and then process it in a CGI script. The HTML form might live in a file named­ and look like this:

    <h1>HSW Survey Form<h1>
    Enter Your Name:
    <input name="Name" size=20 maxlength=50>
    <P>Enter your sex:
    <input type=radio CHECKED name=sex value=MALE>Male
    <input type=radio name=sex value=FEMALE>Female
    <P>Select your age<br>
    <SELECT size=2 NAME=age>
      <OPTION> 1-10
      <OPTION> 11-20
      <OPTION> 21-30
      <OPTION> 31-40
      <OPTION> 41-50
      <OPTION> 51-60
      <OPTION> 61 and up
    <P>Enter Your Comment:
    <input name="Name" size=40 maxlength=100>
    <INPUT TYPE=submit value="Submit">
    <INPUT TYPE=reset value="Reset">

The CGI script referenced by this form will receive four different pieces of data: the name, age, sex and comment of the reader who submits the form. The script will have to parse out the four values and handle all of the character transformations. A separate file called was used to create the script survey.cgi and is perhaps 100 lines long.