Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i need some help.

Status
Not open for further replies.

grayliner

Programmer
Feb 2, 2002
10
0
0
US
ok so i've been working on this script for like 3 hours total now and its a simple script, and i think i made a dumb mistake a can't find. so here is it take a look.

#!/usr/bin/perl
#super.cgi - saves form data to a file, and creates a dynamic web page
print "Content-type: text/html\n\n";
use CGI qw:)standard);
use strict;

#declare variables
my ($game, $commercial @records);
my @game_count = (0,0,0);
my %comm_count = ("Budweiser", 0,
"FedEx", 0,
"MasterCard", 0,
"Pepsi", 0);

#assign input items to variables
$game = param( 'game' );
$commercial = param( 'commercial' );

#save form data to a file
open(OUTFILE, ">>", "survey.txt")
or die "Error opening survey.txt. $!, stopped";
print OUTFILE "$game,$commercial\n";
close(OUTFILE);

#calculate survey statistics
open(INFILE, &quot;<&quot;, &quot;survey.txt&quot;)
or die &quot;Error opening survey.txt. $! stoppped&quot;;
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($game, $commercial) = split(/,/, $rec);
$game_count[$game] = $game_count[$game] + 1;
$comm_count{$commercial} = $comm_count{$commercial} + 1;
}


#generate HTML acknowledgement
print &quot;<html><head><title>WKRK-TV</title></head>\n&quot;;
print &quot;<body>\n&quot;;
print &quot;<h2>Thank you for participating in our survey.</h2>\n&quot;;
print &quot;<em><b>What did you think of the Super Bowl game?</em></b>\n&quot;;
print &quot;<table> \n&quot;;
print &quot;<tr><td>It was a great game.</td> <td>$game_count[0]</td></tr>\n&quot;;
print &quot;<tr><td>It was a boring game.</td> <td>$game_count[1]</td></tr>\n&quot;;
print &quot;<tr><td>I didn't watch the game.</td> <td>$game_count[2]</td></tr>\n&quot;;
print &quot;</table><br>\n&quot;;

print &quot;<em><b>Vote for your favorite Super Bowl Commerical:</em></b>\n&quot;;
print &quot;<table>\n&quot;;
foreach my$key (&quot;Budweiser&quot;, &quot;FedEx&quot;, &quot;MasterCard&quot;, &quot;Pepsi&quot;) {
print &quot;<tr><td>$key</td> <td>$comm_count{$key}</td></tr>\n&quot;;
}

print &quot;</table>\n&quot;;
print &quot;</body></html>\n&quot;;


the website its on it &quot;
 
Here's the first one:

my ($game, $commercial @records);

You forgot the comma after the $commercial variable.

should be:

my ($game, $commercial, @records);

Next, open(INFILE, &quot;<&quot;, &quot;survey.txt&quot;)? I think it's right but I never used that way, so try it my way:

open(INFILE, &quot;<survey.txt&quot;);

That's all I can find. To make you HTML coding faster, try it this way:

print <<EOF;

#normal HTML code here

<HTML>
<HEAD>
....


EOF





There is no Knowledge that is not power.
Age: 16
E-mail: projectnet01@yahoo.com
School: Coral Springs High (Company:(not done yet) :)
Status: Currently working with C++ for game developing. And making a musical band.
-Aaron
 
#Debugged this for you but you have a few undeclared #varibles left, but should work fine when they get passed
#you should be able to cut and paste this
#!/usr/bin/perl
#super.cgi - saves form data to a file, and creates a #dynamic web page
print &quot;Content-type: text/html\n\n&quot;;
use CGI qw:)standard);
use strict;

#declare variables
my ($game, $commercial, @records);
my @game_count = (0,0,0);
my %comm_count = (&quot;Budweiser&quot;, 0,
&quot;FedEx&quot;, 0,
&quot;MasterCard&quot;, 0,
&quot;Pepsi&quot;, 0);

#assign input items to variables
$game = param( 'game' );
$commercial = param( 'commercial' );

#save form data to a file
open(OUTFILE, &quot;>>survey.txt&quot;)
or die &quot;Error opening survey.txt. $!, stopped&quot;;
print OUTFILE &quot;$game,$commercial\n&quot;;
close(OUTFILE);

#calculate survey statistics
open(INFILE, &quot;<survey.txt&quot;)
or die &quot;Error opening survey.txt. $! stoppped&quot;;
@records = <INFILE>;
close(INFILE);
foreach my $rec (@records) {
chomp($rec);
($game, $commercial) = split(/,/, $rec);
$game_count[$game] = $game_count[$game] + 1; #<---undeclared variable and is it a numeric varible
$comm_count{$commercial} = $comm_count{$commercial} + 1;#<---undeclared variable and is it a numeric varible
}


#generate HTML acknowledgement
print &quot;<html><head><title>WKRK-TV</title></head>\n&quot;;
print &quot;<body>\n&quot;;
print &quot;<h2>Thank you for participating in our survey.</h2>\n&quot;;
print &quot;<em><b>What did you think of the Super Bowl game?</em></b>\n&quot;;
print &quot;<table> \n&quot;;
print &quot;<tr><td>It was a great game.</td> <td>$game_count[0]</td></tr>\n&quot;;
print &quot;<tr><td>It was a boring game.</td> <td>$game_count[1]</td></tr>\n&quot;;
print &quot;<tr><td>I didn't watch the game.</td> <td>$game_count[2]</td></tr>\n&quot;;
print &quot;</table><br>\n&quot;;

print &quot;<em><b>Vote for your favorite Super Bowl Commerical:</em></b>\n&quot;;
print &quot;<table>\n&quot;;
foreach my $key (&quot;Budweiser&quot;, &quot;FedEx&quot;, &quot;MasterCard&quot;, &quot;Pepsi&quot;) {
print &quot;<tr><td>$key</td> <td>$comm_count{$key}</td></tr>\n&quot;;
}

print &quot;</table>\n&quot;;
print &quot;</body></html>\n&quot;;
 
add this to the top of your cgi scripts and it will help you to debug.

use warnings;
use CGI::Carp qw(fatalsToBrowser);

warnings is a pragma that enables all warnings and the second is a module that will print warnings to your browser.
 
Be sure to pull the call to CGI::Carp when you deploy your script, as any remaining problems will then be visible to anyone running your script. Some folks consider this to be a security risk because it reveals information about your site and could lead to exploits.
 
Yeah I should have mentioned that.... thanks footpad... also use warnings is new to perl 5.6.1 if using a version earlier than that use the -w switch as in:
#!/usr/bin/perl -w
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top