I'm having some issues getting this coding ironed out. I know there's something I'm not doing right, but cannot figure out exactly what the something is.

Here's the html code:

<!WMS Temp Conversion>
<html>
<head><title>Washington Middle School Temperature Conversion</title></head>

<body>

<h3>Use the form below to convert the temperature of your choice.</h3><br />

<form action="http://localhost/cgi-bin/chap07/c07case1.cgi" method=post>
<p><input type="text" name="temp" size="6" maxlength="5"><br /><br />
<input type="radio" name="celToFah" value="celToFah">Convert from Celsius to Fahrenheit<br />
<input type="radio" name="fahToCel" value="fahToCel">Convert from Fahrenheit to Celsius</p>
<input type="submit" value="Convert">
</form>

</body>
</html>

and here's the perl script:

#!C:/Perl/bin/perl.exe
#c07case1.cgi --Temp Conversion

print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;


#variables
my ($temp, $celToFah, $fahToCel, $fahConversion, $celConversion);
$temp = param('temp');


#calculate
if $celToFah eq "true" {
	convertFromCel();
	$fahConversion = $temp * 9 / 5 + 32;
}
else {
	convertFromFah();
	$celConversion = ($temp - 32) * 5 / 9;
}
exit;

convertFromCel {
	print "<html>\n";
	print "<head><title>Conversion from Celsius</title></head>\n\n";
	print "<body>\n";
	print "<h3>$temp degrees Celsius converts to $fahConversion degrees Fahrenheit.</h3>\n";
	print "</body>\n";
	print "</html>";
		}

convertFromFah {
	print "<html>\n";
	print "<head><title>Conversion from Fahrenheit</title></head>\n\n";
	print "<body>\n";
	print "<h3>$temp degrees Fahrenheit converts to $celConversion degrees Celsius.</h3>\n";
	print "</body>\n";
	print "</html>";
		}

Recommended Answers

All 9 Replies

Is it not being able to find perl path ?else it is just a simple temperature conversion program.

Actually, it's saying that there are syntax errors on lines 15 near "if $celToFah" & 18 near "}". I'm not sure what they are, though.

This is a stretch, since I'm not in front of linux/unix computer right now, but the error might be at your subroutines. I always do this, so I don't know if it's a problem, but you might try doing:
sub convertFromCel, rather than just convertFromCel at the end when you define your subroutines.
Also, I believe you need a semi-colon at the end of your subroutines after the outside closing bracket.

Hope my shoddy memory has been of help :)

, Mike

I tried that and it made no difference.

I think there's something that I'm doing wrong with the

if $celToFah eq "true" {
	convertFromCel();
	$fahConversion = $temp * 9 / 5 + 32;
}

part. I'm not sure that I'm stating correctly that if "this radio button is checked, do this..."

this line:

if $celToFah eq "true" {

needs parenthesis:

if ($celToFah eq "true") {

But since $celToFah has no value it will never equal "true". If there is a radio button in the form you have to import it's value to your script using the param() function just like you did for:

$temp = param('temp');

or you can do this:

if (param('celToFah')) [
   do this
}
else {
   do this other thing
}

you don't really need to checks if value since it can only have one value, you just need to check that it has any value which the above does.

Arrgh! No matter which radio button is selected, it keeps converting from F to C. It refuses to convert from C to F. What on earth am I missing here?

#!C:/Perl/bin/perl.exe
#c07case1.cgi --Temp Conversion

print "Content-type: text/html\n\n";
use CGI qw(:standard);
use strict;

#variables
my ($temp, $convMeth, $fahConversion, $celConversion);
$temp = param('temp');
$fahConversion = $temp * 9 / 5 + 32;
$celConversion = ($temp - 32) * 5 / 9;

#calculate
	if (param('celToFah')) {
		convertFromCel();
	}
	else {
		convertFromFah();
	}

sub convertFromCel {
	print "<html>\n";
	print "<head><title>Conversion from Celsius</title></head>\n\n";
	print "<body>\n";
	print "<h3>$temp degrees Celsius converts to $fahConversion degrees Fahrenheit.</h3>\n";
	print "</body>\n";
	print "</html>";
		}

sub convertFromFah {
	print "<html>\n";
	print "<head><title>Conversion from Fahrenheit</title></head>\n\n";
	print "<body>\n";
	print "<h3>$temp degrees Fahrenheit converts to $celConversion degrees Celsius.</h3>\n";
	print "</body>\n";
	print "</html>";
		}

Probably the celToFah param isn't being passed, since it's always going with the default.

Check the form and make sure the param name is set correctly.

Worst case, do some in-script debugging and print out %ENV on your result page and you should see what keys and values are actually getting passed.

It might be something as simple as upper v. lower case letters

Hope this helps :)

, Mike

works OK for me.

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.