Save Form
This is the basic CGI script for processing Web forms. The script will
take the information from the Web form and save it to a tab-delimited
text file, responses.txt, in your public_html directory while printing
a brief message to the Web browser.
Perl has a special way of accessing files called file handles. By defining
a file handle, it is easy to read from or print to a file.
Open a text editor to write the script:
pico saveform.cgi
Type the following Perl code in your text editor exactly as it appears
here:
#!/usr/local/bin/perl
use CGI;
CGI::ReadParse(*in);
$file = '../tutorial_files/responses.txt';
print "Content-type:text/html\n\n";
print '<HTML><HEAD><TITLE>saveform.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
print '<h2>Thank you for your form submission.</h2>';
print '</BODY></HTML>';
open(FILE, ">>$file")||die "Can't open $file";
flock(FILE, 2)||die "Can't lock $file";
foreach $i (keys %in) {
print FILE "$in{$i}\t";
}
print FILE "\n";
close(FILE) || die "Can't close $file";
Save the file and exit the editor.
Make the script executable, type:
chmod 755 $HOME/public_html/cgi-bin/saveform.cgi
This table includes a line-by-line explanation of saveform.cgi:
| #!/usr/local/bin/perl |
Tells the server the Perl interpreter is located in the directory
/usr/local/bin/perl |
| use CGI; |
Loads the CGI
module. |
| CGI::ReadParse(*in); |
A method from the CGI
module that takes the form field information and saves it as the
hash %in.
The formfield names are the keys of the hash. |
| $file = '../tutorial_files/responses.txt'; |
Assigns the path of the file used to store the form information
to the variable $file. |
print "Content-type:text/html\n\n";
print '<HTML><HEAD><TITLE>saveform.cgi results</TITLE></HEAD>';
print '<BODY BGCOLOR="FF0066">';
print '<h2>Thank you for your form submission.</h2>';
print '</BODY></HTML>'; |
These print statements start the HTML document, create the <HEAD>
section, and begin the <BODY> section of the HTML page. |
open(FILE, ">>$file")||die "Can't open $file";
flock(FILE, 2)||die "Can't lock $file"; |
Defines the filehandle
FILE and opens the file being used to store the form information.
If that fails, an error message is printed.
The >> indicates the file can be appended when it is
opened. die is a command that instructs the script
to exit with the error message indicated between quotes. flock(FILE,
2) is a command that prevents two scripts from accessing the
file at the same time. The 2 creates an exclusive lock and
is used when writing to a file. |
foreach $i (keys %in) {
print FILE "$in{$i}\t";
} |
This foreach
loop is similar to the one used in the script echoform.cgi.
Using the filehandle, FILE, after the word print indicates
that what is enclosed in quote marks is to be printed to the file
indicated by the filehandle. \t is a special
character in Perl; it will create a tab in the file. |
| print FILE "\n";> |
This print statement prints a newline character to the file; a newline
character is needed so the information from each new form is on a
separate line. |
| close(FILE) || die "Can't close $file"; |
Tells Perl to close the file indicated by the filehandle FILE;
if the file can't be closed, the script exits printing the error message
enclosed in quotes after 'die'. |
|