Home

Help

Search


CGI Forms

by Helen Bradley and John Hilvert

In May's column we looked at creating client side forms for your Web site to allow you to get information from your viewers. There are two ways of handling forms on your web site; client side and server side. The processing for client side forms is handled by your computer and browser whereas server side forms are handled by the server that stores your page.

With a server side form when the user clicks on the Send button, the information from the form is sent to the server which then processes it. A program on the server orders the information and then either stores it, emails it or does whatever the program code has been designed to do with it. This program for processing a form is usually written in Common Gateway Interface (CGI) script which is a scripting language that really requires a knowledge of Unix computing to use. The good news is that you don't have to learn CGI scripting to create server side forms, other people have done the hard work already and you can download their scripts from the WWW.

This month we'll show you how you can use FormMail from Matt's script archives to create a server side form on your web site.

   

The format of the email from formmail.pl is neat and customisable using form fields in your HTML source.


Matt's Script Archives is a good place to find CGI scripts for visitor counters, guest books, search engines and many other handy uses.


WS_FTP LE provides a handy CHMOD option which eliminates the need to use Telnet to set the file permissions for your script file.

Before you begin
Before you can set up your form you need to know something about the server that hosts your web site. We are assuming here that you don't run your own server and that you have your site loaded on someone else's server.

You need to know the operating system that your server uses, in most cases this will be Unix although there is a version of FormMail for WindowsNT (see box) if your server uses this. You will also need permission from whoever manages your server (Webmaster) to use a CGI program on their machine. This is where things can begin to be difficult and you become somewhat reliant on the goodwill of your Webmaster. Some will be quite happy about using well known scripts such as Matt's on their server but others may charge you for their time examining the code to make sure it is safe to run on their machine. We experienced both ends of the scale in researching this article, Magnadata could not have been more helpful but another Webmaster quoted $100 per hour for checking the script before allowing us to put it on the server. At this price, you need to decide just how much you need to use server side forms. While a business may opt to pay, for many hobbyists this may far too expensive an option.

Given that you can use CGI scripts, your next task is to locate an FTP program and to get an FTP account on your server or you will need to arrange for your Webmaster to put the script up for you. This should be a simple step as you probably already have an FTP account and software that you use to upload your HTML files.

If you'll be uploading the script files yourself then you'll need a Telnet program to execute Unix commands on your server which set up the FormMail program for use (for Telnet software sources, see box). The Telnet program needs a login ID and password which will be the same ones that you use for your FTP account.

 

   
What if your server isn't running Unix
If your server is running another operating system such as WindowNT you won't necessarily be able to run Matt's FormMail script. As other operating systems increase in popularity his scripts are being adapted for use on these so Matt's site is still a good place to begin your search. Just look for scripts that are compatible with WindowsNT.



You and your webmaster
Researching this article gave us an opportunity to discover just how important your Webmaster can be when you start moving away from simple HTML and into the realms of using server side scripts. The team at Magnadata were extremely helpful as was the team at InterNet Australis who assisted where they could in getting FormMail running on an NT server - thanks!

If you set it up correctly you can use the formmail.pl script on a different server to the one hosting the .htm file containing the form. So, if your Webmaster isn't cooperative, if you have a problem getting CGI-BIN access or clearing forms, or if your server runs WindowsNT and can't run the FormMail script, then don't give up. Instead, find a provider that is willing to host your formmail.pl script. Your .htm file containing your form will reside on your existing server and the form's action will point to the formmail.pl file on the server hosting your CGI script.

How to get the script
You will find Matt's FormMail script available for downloading at:

http://worldwidemart.com/scripts/

Unzip the file Formmail.zip and you'll find two files formmail.pl - the script file itself and a readme file which you should read carefully. You will see that you need to know where your server's sendmail program is. You can find this by using Telnet to login to your server and typing:

which sendmail

This should tell you where sendmail is on your server. The script is set up for it to be in the /usr/lib directory so if it's in a different location you'll need to alter the script file.

If you can't find sendmail on your server this way then speak to your Webmaster. We couldn't find it on our server using the 'which sendmail' command even though it was there but our Webmaster was able to tell us where it was when emailed with our request.

You will also need to know where your Perl interpreter is on the server. It is most likely to be in the directory /usr/bin/perl (which is the default in the script file) but you should check when you are connected to your server using your Telnet program by typing:

which perl

and write down the directory if it differs from the default.

Now, armed with the knowledge of where sendmail and your Perl interpreter are on your server, open formmail.pl in NotePad or another text editor that will save files as plain text. The first line of the script is the location of your perl interpreter. It currently reads #!/usr/bin/perl, and you should alter this if needed so it points to the directory that the Perl interpreter is located in on your server. About a half page further down is the line: $mailprog = '/usr/lib/sendmail'; which is the location of sendmail, again, alter this if necessary.

Edit the line:

@referers = ('www.worldwidemart.com','worldwidemart.com','206.31.72.203');

replacing the IPs with the IPs of your server.

You can now save file formmail.pl as a text file and upload it using FTP to your cgi-bin directory which should be located off your personal directory on the server. If you don't have a cgi-bin directory, then create one first.

Now you need to alter the permissions for the file, so Telnet into your server and execute the command:

cd cgi-bin

to change directory to your cgi-bin directory.

Now type:

chmod 755 formmail.pl

This is necessary to allow anyone to read and execute the script file and to allow you to write to it. CHMOD is short for CHange access MODe and is similar to the ATTRIB command in DOS.

You can now exit your telnet session.

   
What if it doesn't work?
If your form doesn't work, then check that: formmail.pl has been saved as a plain text file (straight ASCII) without carriage returns the permissions (CHMOD 755) have been set correctly the variables are correct for your site your form's action refers to the correct location of your formmail.pl script your perl interpreter is in the location assumed by the script sendmail is in the location assumed by the script.



Testing the script
To test the script create a simple form file. There is absolutely no point creating a fancy form at this stage because, if it doesn't work you'll be confused as to where the problem is. A simple form is all you need (see html source box).

If you are adapting our form for your own use, alter this line to refer to your server and the location of your formmail.pl script on it:

<form action="http://www.edutainment.com.au/cgi-bin/formmail.pl" method="POST">

You'll find that the output from the form is much neater than the output you receive from client side forms. Figure 1 shows the sample form itself and the output received as email from it.

When your simple form is working, you can create and test a more complex version. You'll find there are many fields that you can use in your form to control the layout of the form, how it is completed and how the resulting email reply is formatted. The readme file contains information on these and there is additional information on Matt's Archive site too.

You'll find comments throughout the sample form indicating how each form field relates to either the CGI script or the layout of the resulting email message. Using these comments and the information in the readme file you can adapt and enhance this form for your own use.

   
CGI Resources:
A tour of HTML Forms and CGI scripts:
http://www.halcyon.com/
sanford/cgi/cgi-tour

Find a detailed introduction to CGI at:
http://hoohoo.ncsa.uiuc.
edu/cgi/overview.html

Telnet software download sites:
University of Idaho Library:
http://www.lib.uidaho.
edu/download/

WorldNET
http://www.wnet.net.th/
wnet/download/ftelnet.htm

University of Minnesota, telnet for Mac and PC:
http://sciweb.lib.umn.edu/
s&e/db-util.htm

WS-FTP LE download site:
http://www.ipswitch.com/
downloads/index.html
Do I have to use Telnet?
No, you don't have to use Telnet if you are unfamiliar with it, but you will need some other software to set the Unix permissions for your formmail.pl file. The program WS_FTP LE allows you to both FTP your files and set permissions (see box for download information) so you may consider this alternative.

If you are using WS-FTP LE, ftp the formmail.pl file to your CGI-BIN directory and right click on the file. From the menu select CHMOD (Unix), which will bring up the Remote file permissions dialog box from which you need to check seven of the nine boxes. Check all three boxes, read, write and execute for Owner, check read and execute for Group and read and execute for Other. Leave the write permissions for Group and Other unchecked, figure 3 and click OK. You should see site chmod 755 formmail.pl in the lower left status bar of WS_FTP LE.

Why chmod 755?
For the technically interested, the 755 derives from the following binary conventions for the three groups; Owner, Group and Other:

4 = Read permission only
2 = Write permission only
1 = Execute permission only
0 = no permissions

You can combine these to set multiple permissions for each group so:
Owner =read+write+execute=4+2+1=7; Group=read+execute=5; Other=read+execute=5. Thus CHMOD 755 sets the permissions for all three.

Other scripts
Matt's Script Archives offer a range of CGI scripts including a guest book and a visitor counter, see figure 2, so his site is a good place to start. Once you get familiar with the process you'll find there are many other sources of CGI scripts on the WWW, it's just a case of deciding what you want and having a look around for what is available.

Html source for a simple test form
<html>
<head>
<title>PC User CGI email form</title>
</head>
<body bgcolor="#CCCCCC" text="#990033" link="#660000" vlink="#663333">
<h1>Email Form</h1>
<h2>Test Version Only</h2>
<hr>

This form uses a CGI script to email the form to the recipient.<p>
It is a simple test version to allow you to check that it works.<p>

<!-- this specifies the location and name of the CGI script-->
<form action="http://www.edutainment.com.au/cgi-bin/formmail.pl" method="POST">

<!-- this specifies the email address to send the output-->
<input type=hidden name="recipient" value="helen@gips.com.au">

<!-- this specifies the subject of the email message-->
<input type=hidden name="subject" value="PC User CGI form">

<!-- this realname form field allows the user to add their name and
it will be used for the sender information in your email message-->
Your Name:<br>
<INPUT NAME="realname" TYPE="TEXT" SIZE="40"><p>

Email address:<br>
<INPUT NAME="email" TYPE="TEXT" SIZE="40"><p>

Tell us what you think about this web site:<br>
<TEXTAREA NAME="ideas" ROWS=4 COLS=40></TEXTAREA><p>
<input type="submit" value="Send this">
<input type="reset" value="Start Over">

<!-- this adds the email and realname form fields to the body of the email message-->
<input type = hidden name = "print_config" value = "email,realname">
</FORM>
Thanks for filling in the form! We look forward to hearing your opinions.
</body>
</html>

   

Top of page

|What's New | Net Guides | Web Workshop | Net Sites | About PC User |
| Games | Education | General & Business | Online Tools | Utilities |
| Patches & Support files | PC User Interactive |

All text © 1997 Australian Consolidated Press - PC User Magazine