MCCC CIS 177 - Markup Languages

7.0 Forms

Introduction to Forms
HTTP allows you to send almost any type of data across the Internet to a Web server. Web sites use forms to obtain input from users. You can place many different fields into one form for user input. Such input can include user name, address, credit card number, email address, etc. The information entered by the user is then submitted to a server where it is stored and/or processed. To be truly functional, a form requires a Common Gateway Interface (CGI), which is created either server-side or client-side and written in a programming language (i.e. C or Perl). The script runs on the server, typically in a special folder established by the host that has been configured for optimal security. The script performs two important functions: It receives data from the browser, then it processes and formats the data.
CGI Scripts

CGI scripts are commonly available as open-source (free/shareware) on the Internet and generally need to be edited for each individual use. Since they are text-based documents you can open a script in Notepad and perform your own editing. Specifically, each script defines the variables used for capturing data, which are the same variables you use in defining your form elements, the operating system used on the server and the paths to the appropriate programming application. Consult your host to determine the exact paths to the program and if they require that you install the script in a special folder (i.e. cgi-bin or cgi-local).

It is also important to note that when you FTP a CGI script to your server, use must use ASCII mode and not binary mode, otherwise the script will fail to function. In addition, on UNIX servers, you must use the UNIX chmod to set the permissions of the script so that it is accessible and executable. In the WS_FTP program you can do so by right-clicking on the file after you've uploaded it and choosing chmod. Set the permissions in the dialog that appears. If your host does not allow you to run CGI scripts, many third-party Web sites allow you to run scripts remotely. Check out CGI Resources for more information.

Basic Form Elements

The basic form elements include the <form> tag, which is a container tag requiring a closing tag and specifies the location of the script and the method used by the script.

<form method="post or get" action="http://pathtoscript/cgi-bin/scriptname">

  • post - form data is posted to the URL specified by the action attribute, if the action references an email address. This is the most common method for sending form data. It can send more characters, although sometimes requires more processing by the script.
  • get - form data is appended to the URL for use in a query string.

Within the form tag, you can use a number of elements for capturing user input including the <input>, <select> and <textarea> tags. The <input> and <select> tags allow you to specify the type attribute such as whether it is a text box, radio button or select list. These tags and their attributes are discussed in greater detail in the next section.

Examine a Form and Related Email CGI Script in PERL

  1. Download the script.
  2. View the sample form.
  3. View the source code of the sample form to examine the method, action and other elements.
  4. Open the CGI script in notepad and examine the code. Pay particular attention to the paths and variables.

Examine a Form and Related PHP Script the Populates a MySQL Database

  1. Download the script.
  2. View the sample form.
  3. View the source code of the sample form to examine the method, action and other elements.
  4. Open the PHP script in notepad and examine the code. Pay particular attention to the paths and variables.