160 likes | 177 Views
Sending data, forms and variables. Please use speaker notes for additional information!. #!/usr/bin/perl #grosspwb.cgi - computes gross pay and a dynamic web page print "Content-type: text/html<br><br>"; #avoid undeclared variables use strict; #declare variables
E N D
Sending data, forms and variables Please use speaker notes for additional information!
#!/usr/bin/perl #grosspwb.cgi - computes gross pay and a dynamic web page print "Content-type: text/html\n\n"; #avoid undeclared variables use strict; #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = "Susan Ash"; $payhr = 55.75; $hours = 40; $bonus = 5000; #calculate gross pay $gross = $payhr * $hours; $grossyr = $payhr * $hours * 52 + $bonus; #create gross pay web page print "<html>\n"; print "<head><title>Gross Pay</title></head>\n"; print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name<br>\n"; print "Pay rate: $payhr<br>\n"; print "Hours: $hours<br>\n"; print "Gross pay: $gross<br>\n"; print "Yearly gross pay: $grossyr<br>\n"; print "</body>\n"; print "</html>\n"; By coding use strict, I limit Perl from declaring its on variables. I then define the variables: both the ones that I give a constant value to and the ones that will receive the answers I calculate. I do the calculations using assignment statements.. I print the results on the screen. Note I am printing both a constant and the contents of the variables.
#!/usr/bin/perl #grosspwb.cgi - computes gross pay and a dynamic web page print "Content-type: text/html\n\n"; #avoid undeclared variables use strict; #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = "Susan Ash"; $payhr = 55.75; $hours = 40; $bonus = 5000; #calculate gross pay $gross = $payhr * $hours; $grossyr = $payhr * $hours * 52; $grossyr = $grossyr + $bonus; #create gross pay web page print "<html>\n"; print "<head><title>Gross Pay</title></head>\n"; print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name\n"; print "Pay rate: $payhr\n"; print "Hours: $hours\n"; print "Gross pay: $gross<br>\n"; print "Yearly gross pay: $grossyr<br>\n"; print "</body>\n"; print "</html>\n";
<html> <head><title>Spruce Department Store</title></head> <body> <div align=center> <h1>Get Pay Information</h1> <img src="spruce.gif"> </div> <form action="http://www.pgrocer.com/cgi-bin/begin/grosspar.cgi" method=post> Employee Name: <input name=Employee Size=20><br><br> Pay per Hour: <input name=PayHour Size=5><br><br> Hours Worked: <input name=NumHours Size=5><br><br> Bonus: <input name=Bonus Size=5><br><br> <input type=submit value=Submit><br> <input type=reset value=Reset> </form> </body> </html> The ACTION property points to the CGI that will receive the form data and process it. The METHOD can be either POST or GET. From CGI/Perl by Diana Zak (page 58): “The GET method which is the default value for the METHOD property, appends the form data to the end of the URL specified in the ACTION property and is similar to sending the data using a hyperlink. The server retries the data from the URL and stores it in a test string for processing by the CGI script. The POST method, on the other hand, sends the form data in a separate data stream, allowing the Web server to receive the data through what is called ‘standard input’. Because it is more flexible, the POST method is considered the preferred way of sending data to the server”.
#!/usr/bin/perl #grosspay.cgi - computes gross pay and a dynamic web page print "Content-type: text/html\n\n"; use CGI qw(:standard); #avoid undeclared variables use strict; #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = param('Employee'); $payhr = param('PayHour'); $hours = param('NumHours'); $bonus = param('Bonus'); #calculate gross pay $gross = $payhr * $hours; $grossyr = $payhr * $hours * 52 + $bonus; #create gross pay web page print "<html>\n"; print "<head><title>Gross Pay</title></head>\n"; print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name<br>\n"; print "Pay rate: $payhr<br>\n"; print "Hours: $hours<br>\n"; print "Gross pay: $gross<br>\n"; print "Yearly gross pay: $grossyr<br>\n"; print "</body>\n"; print "</html>\n"; Note that the names in the parenthesis after param are the same names that were in used in input name on the html form on the previous slide. These names are passed when the information from the form is passed.
From grosspost.html <form action="http://www.pgrocer.com/cgi-bin/begin/grosspar1.cgi" method=post> Employee Name: <input name=Employee Size=20><br><br> Pay per Hour: <input name=PayHour Size=5><br><br> Hours Worked: <input name=NumHours Size=5><br><br> Bonus: <input name=Bonus Size=5><br><br> <input type=submit value=Submit><br> <input type=reset value=Reset> </form> From grosspar.cgi #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = param('Employee'); $payhr = param('PayHour'); $hours = param('NumHours'); $bonus = param('Bonus');
<html> <head><title>Spruce Department Store</title></head> <body> <div align=center> <h1>Get Pay Information</h1> <img src="spruce.gif"> </div> <form action="http://www.pgrocer.com/cgi-bin/begin/grosspar.cgi" method=get> Employee Name: <input name=Employee Size=20><br><br> Pay per Hour: <input name=PayHour Size=5><br><br> Hours Worked: <input name=NumHours Size=5><br><br> Bonus: <input name=Bonus Size=5><br><br> <input type=submit value=Submit><br> <input type=reset value=Reset> </form> </body> </html> Uses the get method.
my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = param('Employee'); $payhr = param('payHour'); $hours = param('NumHours'); $bonus = param('Bonuss'); I changed the names of two of the pieces of information being passed. First, payHour should be PayHour and Bonuss should be Bonus. See previous examples to compare. Notice PayHour and Bonus are being passed but they cannot be received into payHour and Bonuss.
25 x 40 = 1000 so gross pay is okay 1000 x 52 = 52000 If the bonus was added it would be 54555 so clearly the bonus is still a problem. $name = param('Employee'); $payhr = param('PayHour'); $hours = param('NumHours'); $bonus = param('Bonuss');
print "<html>\n"; print "<head><title>Gross Pay</title></head>\n" print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name<br>\n" print "Pay rate: $payhr<br>\n"; print "Hours: $hours<br>\n"; print "Gross pay: $gross<br>\n"; print "Yearly gross pay: $grossyr<br>\n"; print "</body>\n"; print "</html>\n"; I removed the ; from the two places shown.
#!/usr/bin/perl #grosspay.cgi - computes gross pay and a dynamic web page print "Content-type: text/html\n\n"; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); #avoid undeclared variables use strict; #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = param('Employee'); $payhr = param('PayHour'); $hours = param('NumHours'); $bonus = param('Bonus'); #calculate gross pay $gross = $payhr * $hours; $grossyr = $payhr * $hours * 52 + $bonus; #create gross pay web page print "<html>\n"; print "<head><title>Gross Pay</title></head>\n" print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name<br>\n" print "Pay rate: $payhr<br>\n"; print "Hours: $hours<br>\n"; print "Gross pay: $gross<br>\n"; print "Yearly gross pay: $grossyr<br>\n"; print "</body>\n"; print "</html>\n";
print "Employee: $name<br>\n"; print "Pay rate: \$$payhr<br>\n"; print "Hours: $hours<br>\n"; print "Gross pay: \$$gross<br>\n"; print "Yearly gross pay: \$$grossyr<br>\n";
#!/usr/bin/perl #grosspay.cgi - computes gross pay and a dynamic web page print "Content-type: text/html\n\n"; use CGI qw(:standard); #avoid undeclared variables use strict; #declare variables my($name, $payhr, $hours, $bonus, $gross, $grossyr); $name = param('Employee'); $payhr = param('PayHour'); $hours = param('NumHours'); $bonus = param('Bonus'); #calculate gross pay $gross = $payhr * $hours; $grossyr = $payhr * $hours * 52 + $bonus; #create gross pay web page print "<html>\n"; print "<head><title>Gross Pay</title></head>\n"; print "<h1>Calculate Gross Pay</h1>\n"; print "<body>\n"; print "Employee: $name<br>\n"; printf "Pay rate: \$%.2f<br>\n",$payhr; print "Hours: $hours<br>\n"; printf "Gross pay: \$%.2f<br>\n",$gross; printf "Yearly gross pay: \$%.2f<br>\n",$grossyr; print "</body>\n"; print "</html>\n";