LJ Archive

Listing 1

#!/usr/local/bin/perl5

# We want to use the CGI module
use CGI;

# Set the filename to which we want the elements saved
my $filename = "/home/reuven/Consulting/guestbook.html";

# Set the character that will separate fields in the file
my $separation_character = "</P><P>";

# In what order do we want to print fields?
my @fields = ("name", "email");

# Which fields are required?
my @required_fields = ("name", "email");

# What is the full name for each required field?
$FULLNAME{"name"} = "your full name";
$FULLNAME{"email"} = "your e-mail address";

# Create a new CGI object
my $query = new CGI;

# ------------------------------------------------------------
# Make sure that all required fields have arrived
foreach $field (@required_fields)
{
    # Add the name of each missing field
    push (@missing_fields, $field) if ($query->param($field) !~ m/\w/);
}

# If any fields are missing, invoke the error routine
&missing_field(@missing_fields) if @missing_fields;

# ------------------------------------------------------------
# Open the file for appending
open (FILE, ">>$filename") || &error_opening_file($filename);

# Grab the elements of the HTML form
@names = $query->param;

# Iterate through each element from the form, writing each element to
# $filename.  Separate elements with $separation_character defined
# above.
foreach $index (0 .. $#fields)
{
    # Get the input from the appropriate HTML form element
    $input = $query->param($fields[$index]);

    # Remove any instances of $separation_character
    $input =~ s/$separation_character//g;

    # Now add the input to the file
    print FILE $input;

    # Don't print the separation character after the final element
    print FILE $separation_character if ($index < $#fields);
}

# Print a newline after this user's entry
print FILE "<BR><HR><P>\n\n";

# Close the file
close (FILE);

# ------------------------------------------------------------
# Now thank the user for submitting their information

# Print an a appropriate MIME header
print $query->header("text/html");

# Print a title for the page
print $query->start_html(-title=>"Thank you");

# Print all of the name-value pairs
print "<P>Thank you for submitting the form.</P>\n";
print "<P>Your information has been saved to disk.</P>\n";

# Finish up the HTML
print $query->end_html;


# ------------------------------------------------------------
# Subroutines

sub missing_field
{
    # Get our local variables
    my (@missing_fields) = @_;

    # Print an a appropriate MIME header
    print $query->header("text/html");

    # Print a title for the page
    print $query->start_html(-title=>"Missing field(s)");

    print "<P>You are missing the following required
fields:</P>\n";
    print "<ul>\n";

    # Iterate through the missing fields, printing them
    foreach $field (@missing_fields)
    {
        print "<li> $FULLNAME{$field}\n";
    }

    print "</ul>\n";

    print "</ul>\n";

    # Finish up the HTML
    print $query->end_html;

    exit;
}


sub error_opening_file
{
    my ($filename) = @_;

    # Print an a appropriate MIME header
    print $query->header("text/html");

    # Print a title for the page
    print $query->start_html(-title=>"Error opening file");

    # Print the error
    print "Could not open the file \"$filename\".</P>\n";

    # Finish up the HTML
    print $query->end_html;

    exit;
}
LJ Archive