CGI Scripts to Create a Website with Interactive Features
Take a Poll
There are 2 scripts in this exercise. The first presents a form with radio buttons. The second script processes the information and displays the results to the screen.
opinion_poll1.pl
This script produces the form that gathers the polling information. The action statement of the form specifies the second script rate_op1.pl
#!/usr/bin/perl
#opinion_poll1.pl
print "Content-type:text/html\n\n"; #Follow with blank line
print "<html>\n";
print "<head><title>Opinion Poll #1</title>\n";
print "<style type=\"text/css\">\n";
print "h2\{font-family: Arial;font-style : normal ;font-size : 12pt; font-weight :bold;text-align :center \}\n";
print "p\{font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :left \}\n";
print "form\{font-family: Arial;font-style : normal ;font-size : 9pt; font-weight :normal;text-align :left\}\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<h2>Pop up Advertising</h2>\n";
print "<p>It seems that the practice of Pop Up Advertising is a growing trend. This type of advertising takes an in-your-face aggressive approach to presenting information to an internet visitor. We've presented a few contrasting opinions on the subject below. Choose one that best expresses your feelings.</p>\n";
print "<form method=\"POST\" action=\"rate_op1.pl\">\n";
print "<hr>\n";
print "<input type=\"radio\" name=\"op1\" value=\"val1\"><p>I really like this type of advertising. Even if I have to close a pop up every couple of seconds it gives me the option of accepting or rejecting the advertisement.\n";
print "<hr>\n";
print "<input type=\"radio\" name=\"op1\" value=\"val2\"><p>I don't even notice pop up advertisements. I just close them as they open and go on my way.\n";
print "<hr>\n";
print "<input type=\"radio\" name=\"op1\" value=\"val3\"><p>I think this is the most aggravating form of advertisement on the internet.\n";
print "<hr>\n";
print "<input type=\"submit\" value=\"Log My Opinion\"><input type=\"reset\">\n";
print "</form>\n";
print "</body>\n";
print "</html>\n";
Try the Scripts
rate_op1.pl
This script parses the information and appends the result to a file. It then reopens the file and calculates the overall percentages of the poll. It displays the results in a table format.
#!/usr/bin/perl
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /;
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
$buffer =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$buffer =~ s/<!--(.|\n)*-->/ /g;
$buffer =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$op1=$formdata{'op1'};
open (INFO,">>op1.txt"); #For Appending
print INFO "$op1\n";
close (INFO);
open (INFO,"op1.txt"); #for Reading
@array=<INFO>;
close (INFO);
chomp (@array);
$count=0;
$num1=0;
$num2=0;
$num3=0;
foreach $line (@array) {
$count++;
if($line =~ /val1/){
$num1++;
}
if($line =~ /val2/){
$num2++;
}
if($line =~ /val3/){
$num3++;
}
}
$percent1=($num1/$count)*100;
$percent2=($num2/$count)*100;
$percent3=($num3/$count)*100;
$per1=sprintf("%4.2f",$percent1);
$per2=sprintf("%4.2f",$percent2);
$per3=sprintf("%4.2f",$percent3);
print "Content-type:text/html\n\n"; #Follow with blank line
print "<html>\n";
print "<head><title>Results Opinion #1</title>\n";
print "<style type=\"text/css\">\n";
print "h2\{font-family: Arial;font-style : normal ;font-size : 12pt; font-weight :bold;text-align :center \}\n";
print "p\{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :left\}\n";
print "td\{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :right\}\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<div align=\"center\">\n";
print "<h2>Results Opinion #1 Pop Up Advertising</h2>\n";
print "<table width=\"400\" cellpadding=\"5\" cellspacing=\"0\" border=\"1\">\n";
print "<tr><th>Percent</th><th>Opinion</th></tr>\n";
print "<tr align=\"center\" valign=\"top\">\n";
print "<td align=\"right\" colspan=\"1\" rowspan=\"1\">$per1</td>\n";
print "<td align=\"left\" colspan=\"1\" rowspan=\"1\"><p>I really like this type of advertising. Even if I have to close a pop up every couple of seconds it gives me the option of accepting or rejecting the advertisement.</td>\n";
print "</tr>\n";
print "<tr align=\"center\" valign=\"top\">\n";
print "<td align=\"right\" colspan=\"1\" rowspan=\"1\">$per2</td>\n";
print "<td align=\"left\" colspan=\"1\" rowspan=\"1\"><p>I don't even notice pop up advertisements. I just close them as they open and go on my way.</td>\n";
print "</tr>\n";
print "<tr align=\"center\" valign=\"top\">\n";
print "<td align=\"right\" colspan=\"1\" rowspan=\"1\">$per3</td>\n";
print "<td align=\"left\" colspan=\"1\" rowspan=\"1\"><p>I think this is the most aggravating form of advertisement on the internet.</td>\n";
print "</tr>\n";
print "</table>\n";
print "<br>\n";
print "<a href=\"http://www.createafreewebsite.net\">Return to Main Page</a>\n";
print "</div>\n";
print "</body>\n";
print "</html>\n";
|