I am having trouble with my code. The conversion for both Fahrenheit and Celsius will not display, and when I select "Celsius to Fahrenheit", it gives me the conversion for Fahrenheit to Celsius. I'm not sure what I'm doing wrong. I've already searched for help and have gotten nowhere. So any help would be appreciated!

cgi script:

#!/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');
$fahConvert = param('fah');
$celConvert = param('cel');

#calculate
    if (param('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";
        }

HTML script:

<!c07case1.html>
<HTML>
<HEAD><TITLE>Washington Middle School</TITLE></HEAD>
<BODY>
<H1>Temperature Converter</H1>
<HR>
<FORM ACTION="https://crux.baker.edu/~wmorni01/cgi-bin/chap07_wendy_morningstar/c07case1.cgi" METHOD=POST>
<P><B>Temperature:</B> <INPUT TYPE=text NAME=temp></P>

<INPUT TYPE=radio NAME=Type Value=fah CHECKED>Fahrenheit to Celsius<BR>
<INPUT TYPE=radio NAME=Type Value=cel>Celsius to Fahrenheit<BR>

<P><INPUT TYPE=submit VALUE=Convert></P>
</FORM></BODY></HTML>

I'm really hoping it's something very simple...

Hello,

Your running the calculation after you print the output. You need to do the calculation then call the print routine. In the perl script swap lines 14 and 15 and lines 19 and 20:

#calculate
if (param('cel')) {
$celConvert = ($temp - 32) * 5 / 9;
cel();
}
else {
$fahConvert = $temp * 9 / 5 + 32;
fah();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.