Data files using cgi/perl

Post on 05-Feb-2016

20 views 0 download

Tags:

description

Data files using cgi/perl. Please use speaker notes for additional information!. Mailing List Mailing List Enter Name: - PowerPoint PPT Presentation

Transcript of Data files using cgi/perl

Data files using cgi/perl

Please use speaker notes for additional information!

<!mailinglist.html><html><head><title>Mailing List</title></head><body><h1>Mailing List</h1><form action="http://www.pgrocer.com/cgi-bin/data/maillist.cgi" method=post>Enter Name:<br><input type=text name=name size=25><br><br>Enter Address:<br><input type=text name=stadr size=25><br><br>Enter City:<br><input type=text name=city size=20><br><br>Enter State:<br><input type=text name=state size=2<br><br>Enter ZIP:<input type=text name=zip size=10<br><input type=submit value="Submit"><input type=reset value="Reset"></form></body></html>

#!/usr/bin/perl#maillist.cgi - saves name and address information to make a mailing listuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($name, $stadr, $city, $state, $zip);#assign variables$name = param('name');$stadr = param('stadr');$city = param('city');$state = param('state');$zip = param('zip');#save form data to a fileopen(FILEOUT, ">>", "maillist.txt")

or die "file error: maillist.txt. $!, stopped";print FILEOUT "$name, $stadr, $city, $state, $zip\n";close(FILEOUT);print "<html><head><title>Record just entered</title><basefont size=5></head>\n";print "<body>\n";print "<div align=center>\n";print "$name, $stadr, $city, $state, $zip\n";print "</div></body></html>\n";

#!/usr/bin/perl#maillist.cgi - saves name and address information to make a mailing listuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($name, $stadr, $city, $state, $zip);#assign variables$name = param('name');$stadr = param('stadr');$city = param('city');$state = param('state');$zip = param('zip');#save form data to a fileopen(FILEOUT, ">>", "maillist.txt")

or die "file error: maillist.txt. $!, stopped";print FILEOUT "$name, $stadr, $city, $state, $zip\n";close(FILEOUT);print "<html><head><title>Record just entered</title><basefont size=5></head>\n";print "<body>\n";print "<div align=center>\n";print "$name, $stadr, $city, $state, $zip\n";print "</div></body></html>\n";

FILEOUT is opened to append (this is indicated by the >>. The name of the file is maillist.txt. The error line will catch problems opening the file. The record that is written to the file contains all of the fields that were inputted on the html form and then stored in variables in this code.

#!/usr/bin/perl#maillistrd.cgi - reads the mail list fileuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($name, $stadr, $city, $state, $zip, @records);open(INF, "<", "maillist.txt")

or die "file error: maillist.txt. $!, stopped";@records=<INF>;close(INF);print "<html><head><title>Records on mail list</title><basefont size=5></head>\n";print "<body>\n";print "<h1>Mailing list:</h1>\n";foreach my $record (@records) {

chomp($record); ($name, $stadr, $city, $state, $zip) = split(/,/, $record); print "$name, $stadr, $city, $state, $zip<br>\n"; }print "</body></html>\n";

This code reads the records on the maillist.txt file into @records.

I then process @records by going through each record individually using the foreach which establishes $record as an individual record from @records.

<!degreeopts.html><html><head><title>Degree CIS</title></head><body><h1>Degree Options in CIS</h1><form action="http://www.pgrocer.com/cgi-bin/data/degreeopts.cgi" method=post><b>Which option are you enrolled in?</b><br><input type=radio name=option value=0>Webmaster<br><input type=radio name=option value=1>Programming<br><input type=radio name=option value=2>Networking<br><input type=radio name=option value=3>Multimedia and the Internet<br><input type=radio name=option value=4>Business Information Systems<br><input type=radio name=option value=5>Information Systems Transfer<br><input type=radio name=option value=6>Computer Science Transfer<br><input type=submit value="Submit"></form></body></html>

#!/usr/bin/perl#degreeopts.cgi - saves form data to a file and reports resultsuse CGI::Carp qw(fatalsToBrowser);print "Content-type: text/html\n\n";use CGI qw(:standard);use strict;#declare variablesmy ($option, @records);my @options_total = (0, 0, 0, 0, 0, 0, 0);#assign variables$option = param('option');#save form data to a fileopen(FILEOUT, ">>", "totals.txt")

or die "file error: totals.txt. $!, stopped";print FILEOUT "$option\n";close(FILEOUT);

Notice that each record on the file contains only the $option.

#read file and accumulate totals in an arrayopen(FILEIN, "<", "totals.txt")

or die "file error: totals.txt. $!, stopped";@records=<FILEIN>;close(FILEIN);#display contents of array on screenforeach my $record(@records) {

chomp($record); $options_total[$record]= $options_total[$record]+1; }print "<html><head><title>CIS Degree Options</title><basefont=5></head>\n";print "<body>\n";print "<div align=center>\n";print "<h2>Information about degree options in the CIS department.</h2>\n";print "<table width = 50% border=2>\n";print "<tr><td colspan=2>Number of students in CIS Degree options:</td></tr>\n";print "<tr><td>Webmaster</td><td>$options_total[0]</td></tr>\n";print "<tr><td>Programming</td><td>$options_total[1]</td></tr>\n";print "<tr><td>Networking</td><td>$options_total[2]</td></tr>\n";print "<tr><td>Multimedia and the Internet</td><td>$options_total[3]</td></tr>\n";print "<tr><td>Business Information Systems</td><td>$options_total[4]</td></tr>\n";print "<tr><td>Information Systems Transfer</td><td>$options_total[5]</td></tr>\n";print "<tr><td>Computer Science Transfer</td><td>$options_total[6]</td></tr>\n";print "</table>\n";print "</div></body></html>\n";