[ back to toc ]

CGI Script

Date: 2002/03/14 09:53

Q:
I am taking a cgi class in college right now and I have learned absolutely
nothing. Our final is to get checkboxes on our teachers website to
temporarily save information so that we can link to the next persons part.
I do have the script somewhat done but it keeps telling me that I have
script errors. We are using a Unix server but I'm unsure of the version.
I'm going to copy the script on here so that hopefully you can help me.
The error is on line 16 which is under the assign input variables. The
line about the twosets. Well Heres my script I hope you can help me. Thank
you very much.
#!/usr/bin/perl
#final.cgi print "Content-type: text/html\n\n";
use CGI qw(:standard);

sub ReadParse {

#declare variables
my ($twosets, $shortterm, $longterm, @records);

my @twosets_count = ("Short-term Memory Problems","Long-term Memory
Problems");
my %shortterm_count = ("Inconsistent auditory memory", "AUDMEM",
"Inconsistent visual memory", "VISMEM", "Poor memory of physical
sequential tasks", "BOTHMEM","It takes 10-15 repetitions over several days
for an LD child to retain what the average child retains in 3-5
repetitions on one occasion", "REPETITIONS", "Disrupted flow of ideas in
speech.", "DISRSPEECH","Disrupted flow of ideas in writing", "DISRWRIT");
my %longterm_count = ("Long-term memory deficits are a sign of mental
retardation")

#assign input items to variables:
$twosets = param('TWOSETS');
$shortterm = param('SHORTTERM');
$longterm = param('LONGTERM');

#save form data to a file
open(OUTFILE, ">>file.txt")
or die "Error opening survey.txt.";
print OUTFILE "$twosets,$shortterm,$longterm\n";
close(OUTFILE);

#calculate survey statistics
open(INFILE, "<file.txt")
or die "Error opening survey.txt.";
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {

}
chomp($rec);
($twosets, $shortterm, $longterm) = split(/,/, $rec);
$twosets_count[$twosets] = $twosets_count[$twosets] + 1;
$shortterm_count{$shortterm} = $shortterm_count{$shortterm} + 1;
$longterm_count{$longterm} = $longterm_count{$longterm} + 1;

$SENDMAIL="/usr/lib/sendmail -t";

$CHECK_EMPTIES=0;

local (*in) = @_ if @_; local ($i, $loc, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'};}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
@in = split(/&/,$in);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g; ($key, $val) = split(/=/,$in[$i],2);
$FOUND=0; foreach (@FIELDS) { $FOUND=1 if ($key eq $_); }
if (($key ne 'TO')&&($key ne 'SUBJECT')&&($key ne
'REPLY-TO')&&($FOUND==0))
{ push(@FIELDS,$key); }
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val; } return 1; }

&ReadParse;
foreach (@FIELDS) {&MissingInfo if (($in{$_} eq
'')&&($CHECK_EMPTIES==1));}

$SUBJECT=$in{'SUBJECT'}; delete $in{'SUBJECT'};
$REPLYTO=$in{'REPLY-TO'}; delete $in{'REPLY-TO'};

# split up the "to" names for multiple recipients
$TO=$in{'TO'}; @TO=split('\0',$TO);

# Open Sendmail and do our thing
open (SENDMAIL,"| $SENDMAIL") || die "Ain't Gonna Happen";
print SENDMAIL "To: @TO[0]\n";
if ($#TO > 0) { foreach (1..$#TO) { print SENDMAIL "Cc: @TO[$_]\n"; }
delete $in{'TO'}; }
print SENDMAIL "Subject: $SUBJECT\n";
print SENDMAIL "Reply-To: $REPLYTO\n";

# Display each field and responses
foreach (@FIELDS) {
$in{$_}=~ s/\0/\n\n * /g;
print SENDMAIL "---------------------------------------------------\n";
print SENDMAIL "Input Field: $_\n";
print SENDMAIL "Response:\n * $in{$_}\n";
}
print SENDMAIL "---------------------------------------------------\n";
close(SENDMAIL);

# Read in the response and print it.
undef $/; $_=<DATA>; print;

# What to display as the "missing information" error
sub MissingInfo {
print <<EOF;
<HTML>
<HEAD> <TITLE>Error</TITLE> </HEAD>
<BODY>
<H1 ALIGN=CENTER>Error: Fields left empty</H1>
<B>
You are required to fill in all the fields in this response form.
Please go back and fill in the fields with missing information.
</B>
EOF
exit(0);
}

# Everything down here is the default response.
__END__
<HTML>
<HEAD> <TITLE>Thank You</TITLE> </HEAD>
<BODY>
<H1 ALIGN=CENTER>Thank You!</H1>
<B>
Thanks for responding to this form. Your responses have been mailed to
the correct person.<P>
</B>

A:
There is a ; missing on that line.

Regards,
Peter

[ back to toc ]