Navigation

Navigation
Document Actions

Web Forms

How to create a form.

If you're adding a form to a site within the content management system, see the corresponding directions for creating a form.

If your site isn't within the CMS, the School of Medicine provides a template for consistency with the Web Standard.  The Office of Information Systems can provide these files to you, or create the form for you.  OIS provides a very basic cgi script called, form_use.pl, that will send a form's results to an email address or to a flat file. If you would like to use a different script, please contact OIS Client Services at help@med.unc.edu or 966-1325 to talk about loading it into the cgi-bin on the SOM web server.

 

 

The Code

If you know how to create a form in HTML and just need to know how to call to the scripts, you have a couple of choices, as shown below. If you don't know how, please continue down the page for an explanation and examples.

  • Formtest — This script is used to test your form. It will check for duplicate and invalid names and values. It will display a page with all the fields of your form and warn you of any errors. After you finish testing your form, you must change the action statement and use any of the following scripts (formuse1, formuse2, formmail).
    <form action="http://www.med.unc.edu/cgi-bin/etc/formtest.pl" method="POST">
  • Formuse — To mail form results to an email address with no special processing, include this in your form:
    <form method="POST" action="http://www.med.unc.edu/cgi-bin/etc/form_use.pl">
    <input type="hidden" name="form_type" value="mail_to">
    <input type="hidden" name="mail_to" value="email@address">
  • Formuse2 — To process your form as a flat file that will allow you to import the information to an Excel spreadsheet or a database program, such as Access, include this:
    <form method="POST" 
    action="http://www.med.unc.edu/cgi-bin/etc/form_use2.pl">
    <input type="hidden" name="form_type" value="one_line">
    <input type="hidden" name="one_line" value="the path to your results file">
    You must, of course, specify a real file location to the results file. Make sure you use the entire path (for example: /home/users3/jmhahn/wwwpub/results.dat). You will need to create a new file (ie: result.dat) in your directory so that the script has somewhere to write the returned information. Be sure that the permissions of the results file are writeable.
  • Formmail — Another script that will send form results to the specified recipient via email.
    <form action="http://www.med.unc.edu/cgi-bin/formmail/FormMail.pl" method="POST" >
    <input type=hidden name="recipient" value="email@address">
    Be sure to replace "email@address" with the real address of the person the feedback should be sent to!

    The formmail script has optional values that you can define to customize your form.

 [top]

Forms and How They Work

A form is simply an area that contains fields/elements which allow a visitor to enter information: for example, text boxes, drop-down menus or radio buttons. When the visitor clicks on the submit button, the content of the form is usually sent to a program that runs on the server. Such programs are sometimes referred to as: CGI programs. It is these programs that receive the information and decide what to do with it. We have a small variety of CGI scripts available for your use (formtest.pl, formuse.pl, formuse2.pl, and formmail). The scripts are already out on the server for you, you just have to call to them using the "action" property (as shown above).

After users fill in the form, they hit the "submit" button. All fields on the form are being sent. The browser sends the information to the server, and the server returns a friendly message such as "thank you for your submission". Meanwhile, the server takes the information and processes it according to directions contained in the CGI script. The server can do the following with information received from a form:

  • Mail information to an email address (use formuse or formmail).
  • Place information into a flat file so you can move it into an excel spreadsheet or database (use formuse2).
  • Process information for importing already formatted information into a database program such as Microsoft Access.

You can perform the first two methods yourself by including the proper hidden field and action statement (as shown above under The Code). If you want to do more sophisticated things with your form, as in the last example, you'll need the help of OIS. Send an email or call us at 966-1325 for more information.

 

The Form Tag

A form is defined with the <form> tag. The <form> tag tells the browser where the form starts and ends. You can add all kinds of HTML tags between the <form> and </form> tags.

This means that a form can include a table or an image along with any of the form fields (which we'll discuss in a minute). In fact, tables are often used to organize the questions and form fields of a form. If you find a form/a web page that you like and want to see how it was created you can view the code by selecting View > Source or, depending on which browser you are using it may be View > Page Source.

Also, a web page may include multiple forms. However, forms may not be nested (you cannot create a form within a form). You must end the current form with </form> before you can start a new form.

To let the browser know where to send the content of a submitted form we add these properties to the <form> tag:

  • action=address
  • method=POST or method=GET

The address is the url of the cgi script the content should be sent to. The POST and GET methods are simply two different methods for submitting data to the script.

Idea IconTip: To process a form on the OIS server you must always use POST as your method. You must also make sure to write it in all capital letters. For example:

<form action="http://www.med.unc.edu/cgi-bin/etc/formtest.pl" method="POST">

 

Form Fields

Now you have the basis of your form. All you have to do is fill it with questions and form fields (fields where your visitors can denote their response).

[top]

Related Links

Here are a couple of additional sites that discuss the process of creating a web form.

If you have any problems with your form, please contact Alicia Orth at aorth@med.unc.edu or 843-7928.

[top]

Site-wide Actions
Personal tools