Question

I'm in a Perl class and this is an assignment. We are supposed to create a simple bonus calculator script, as well as another assignment is a gross pay calculator. The user would input Name, X, and Y. The X and Y would be multiplied together, to get the final value of Z.

However when I do this, I get zeros and blanks. This is my script:

#!/usr/bin/perl
#bonus.cgi - calculates a bonus amount and creates a dynamic Web page
#that contains form data and a bonus amount
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);

#prevent Perl from creating undeclared variables
use strict;

#declare variables
my ($name, $sales, $rate, $bonus);

#assign values to variables
$name = param( ' Salesperson' );
$sales = param( ' Sales' );
$rate = param( ' Rate' );

#calculate bonus amount
$bonus = $sales * $rate;

#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>Patton Industries</TITLE><BASEFONT SIZE=5></HEAD>\n";
print "<H1>Bonus Calculation</H1>\n";
print "<BODY>\n";
print "Salesperson: $name<BR>\n";
printf "Your bonus is \$%.2f. <BR><BR>\n", $bonus;
printf "You entered a sales amount of \$%.2f and a  \n", $sales;
printf "bonus rate of  %.1f%%.<BR>\n", $rate * 100;
print "</BODY>\n";
print "</HTML>\n";

Why would the results (when I click submit) be zeros no matter the numbers I put in? Thank you guys.

No correct solution

OTHER TIPS

Your problem is here:

#assign values to variables
$name = param( ' Salesperson' );
$sales = param( ' Sales' );
$rate = param( ' Rate' );

You have spaces preceding those html element names. I doubt you have <input name=" Salesperson">. And if you do, you should remove the preceding space.

Also, it's unnecessary to predeclare your variables. Instead just do the initialization when you do the first assignment. my $name = param('Salesperson');

your using the module wrong.

   Add 
   my $q = CGI->new;

   change 
   $name = $q->param( ' Salesperson' );

Read CGI Perldoc.perl.org

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top