我的代码遇到问题。华氏温度和摄氏温度的转换都不会显示,当我选择“Celsius to Fahrenheit”时,它会将华氏温度转换为摄氏温度。我不确定我做错了什么。我已经寻求帮助而已经无处可寻。所以任何帮助将不胜感激!如果需要,可以使用以下链接:https://crux.baker.edu/~wmorni01/WEB221_HTML/chapxx/c07case1.html
Perl脚本(修订版):
libssh2
HTML脚本(修订版):
#!/usr/bin/perl
#c07case1.cgi - Temperature conversion
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;
#variables
my ($temp, $fahConvert, $celConvert);
$temp = param('temp');
$unit = param('unit');
$fahConvert = param('fah');
$celConvert = param('cel');
#calculate
if ($unit eq 'cel') {
cel();
$celConvert = ($temp - 32) * 5 / 9;
}
else {
fah();
$fahConvert = $temp * 9 / 5 + 32;
}
sub cel {
print "<html>\n";
print "<head><title>Conversion from Celsius</title></head>\n";
print "<body>\n";
print "<h3>Celsius: $temp </h3>\n";
print "<h3>Fahrenheit: $fahConvert </h3>\n";
print "</body>\n";
print "</html>\n";
}
sub fah {
print "<html>\n";
print "<head><title>Conversion from Fahrenheit</title></head>\n";
print "<body>\n";
print "<h3>Fahrenheit: $temp </h3>\n";
print "<h3>Celsius: $celConvert </h3>\n";
print "</body>\n";
print "</html>\n";
}
答案 0 :(得分:1)
您的两个输入都命名为temp
。首先修复它。
<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P>
<INPUT TYPE=radio NAME=unit Value=fah CHECKED>Fahrenheit to Celsius<BR>
<INPUT TYPE=radio NAME=unit Value=cel>Celsius to Fahrenheit<BR>
使用以下内容获取两个字段的值:
my $temp = param('temp'); # Grab the temperature
my $unit = param('unit'); # "fah" or "cel"
使用以下方法检查温度是否以摄氏度为单位:
if ($unit eq 'cel') { ... }
最后,您只计算生成HTML后转换后的值,该HTML使用将用于存储值的变量。您可以简单地颠倒计算和子调用的顺序,但最好是避免全局变量支持参数。
#!/usr/bin/perl
#c07case1.cgi - Temperature conversion
use strict;
use warnings;
use CGI qw(:standard);
sub fah {
my ($cel, $fah) = @_;
print "<html>\n";
print "<head><title>Conversion to Fahrenheit</title></head>\n";
print "<body>\n";
print "<h3>Celsius: $cel </h3>\n";
print "<h3>Fahrenheit: $fah </h3>\n";
print "</body>\n";
print "</html>\n";
}
sub cel {
my ($fah, $cel) = @_;
print "<html>\n";
print "<head><title>Conversion to Celcius</title></head>\n";
print "<body>\n";
print "<h3>Fahrenheit: $fah </h3>\n";
print "<h3>Celsius: $cel </h3>\n";
print "</body>\n";
print "</html>\n";
}
{
print "Content-type: text/html\n\n";
my $temp = param('temp'); # Grab the temperature
my $unit = param('unit'); # "fah" or "cel"
if ($unit eq 'cel') {
cel( $temp, ($temp - 32) * 5 / 9 );
} else {
fah( $temp, $temp * 9 / 5 + 32 );
}
}