Help needed on a temp conversion app

I’m creating a simple temp conversion from Celsius to Fahrenheit with a drop down menu. Having a slight problem with it not outputting anything, not sure what I am missing here.

Any help would be appreciated.

<?php
//defining variable from the post form
$c2f = $_POST['degree']*9/5+32; 
$f2c = ($_POST['degree']-32)*5/9; 
$scale = $_POST['scale'];

if (isset($_POST['degree'])) {
	$c2f = $_POST['degree']*9/5+32;
	$f2c = ($_POST['degree']-32)*5/9;
	
	if ($_POST['degree'] == "") {
	echo "Error: Please input a degree";
	} 
}

	if ($scale === "Celsius"){
	echo "Fahrenheit is " . $c2f;
	}
	if ($scale === "Fahrenheit") {
	echo "Celsius: " . $f2c;
	}
?>

<html>
<head>
<title>Temperature Conversion</title>
<link rel="stylesheet" href="assets/style.css" /> 
</head>

	<body>
		<div class="container" />
		<form action = "" method ="post">
			<h4>Temperature Conversion</h4>
				<p>Degrees:
					<input type="text" name="degree" size="4" />
					<select name="scale"> <option value="celsius">Celsius</option>
					<option value="fahrenheit">Fahrenheit</option>
					</select>
				</p>
				<p>
					<input type="Submit" />
				</p>
		</form>
		</div>
	
	</body>
</html>

I was able to figure out why I wasn’t able to output.

I moved the following below out of the

and into it’s line:
<select name="scale"> <option value="celsius">Celsius</option>
<option value="fahrenheit">Fahrenheit</option>
</select>

However, now an issue appears if the “Degrees” input box is empty (it displays the proper error message as intended) now displays an extra message depending on which drop down selection it’s currently on.

(e.g. If the drop down selection is on Celsius it outputs “Error: Please input a degreeConverted to Fahrenheit: 32” and vice versa if the Fahrenheit drop down is selected - “Error: Please input a degreeConverted to Celsius: -17.777777777778”.)

How do I remove the second unintended output from appearing after the error message?

It’s not perfect and needs improvement, but I think this will help you get you started.

[php] <?php
if (isset($_POST[‘action’]) && $_POST[‘action’] == ‘submitted’) {

$degree = $_POST['degree'];
$scale = $_POST['scale'];
$output = NULL;

if ( $scale == 'Celsius') {
	$c2f = $degree*9/5+32;
	$output = $degree .' Celsius is ' . $c2f . ' Fahrenheit.';	 
} else {
	$f2c = ($degree-32)*5/9;
	$output = $degree .' Fahrenheit is ' . $f2c . ' Celsius.';
}

}
?>

Temperature Conversion
<body>
<div class="container" />
<form action = "" method ="post">
<input type="hidden" name="action" value="submitted">
<h4><?php echo (isset($output)) ? $output : 'Temperature Conversion'; ?></h4>
Degrees:
<input type="text" name="degree" size="4" />
<select name="scale"> 
	<option value="celsius">Celsius</option>
	<option value="fahrenheit">Fahrenheit</option>
</select>
<p>
<input type="Submit" />
</p>
</form>
</div>

</body>
[/php]

Spruced it up a little ;D

[php] <?php
if (isset($_POST[‘action’]) && $_POST[‘action’] == ‘submitted’) {

$degree = $_POST['degree'];
$scale = $_POST['scale'];
$output = NULL;

if ( $scale == 'celsius') {
	$c2f = $degree*9/5+32;
	$output = $degree .' Celsius is ' . $c2f . ' Fahrenheit.';	 
} else {
	$f2c = ($degree-32)*5/9;
	$output = $degree .' Fahrenheit is ' . $f2c . ' Celsius.';
}

}
?>

Temperature Conversion
<body>
<div class="container" />
<form action = "" method ="post">
<input type="hidden" name="action" value="submitted">
<h4>Temperature Conversion</h4>
Degrees:
<input type="text" name="degree" size="4" />
<select name="scale"> 
	<option value="celsius">Celsius</option>
	<option value="fahrenheit">Fahrenheit</option>
</select>
<p>
<input type="Submit" />
</p>
</form>
<p><?php echo (isset($output)) ? $output : NULL; ?></p>
</div>

</body>

[/php]

Cheers mate for the different take on this, appreciate it.

Sponsor our Newsletter | Privacy Policy | Terms of Service