|
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. ![]()
|
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.
|
||||||
|
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. |
||||||
|
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. |
||||||
|
|
||||||
|
|What's New | Net Guides | Web Workshop |
Net Sites | About PC User |
|