Jump to content

new to php, need help with code


zend

Recommended Posts

im new to php and im simply trying to create a converter from Fahrenheit to Celsius and back. I have tried everything and i dont know why whenever it converts it comes up with 32 for Fahrenheit(obviously its coming up with 0 * 32 for some reason) and for Celsius im just getting way wrong numbers.

 

plzzz any help would be greatly appreciated

<HTML>
<BODY>
<form action="m2.php" method"get">
<input type="text" name"degrees" />

<input type="radio" name="CorF" value="a" />
°C
<input type="radio" name="CorF" value="b" />
°F
<input type="submit" />
</form>
<?php
$degrees = $_GET["degrees"];
$temp = $_GET["CorF"];
$fahrenheit = (($degrees * 1. + 32);
$celsius = (($degrees - 32) / 1.;
if ($temp == "a")
echo "The temperature in Fahrenheit is ", $fahrenheit;
if ($temp == "b")
echo "The temperature in Celsius is ", $celsius;
?>
</BODY>

Link to comment
https://forums.phpfreaks.com/topic/110968-new-to-php-need-help-with-code/
Share on other sites

You shouldn't be doing the processing until after the form is submitted:

<?php
if (isset($_GET['submit'])) {
    $degrees = $_GET["degrees"];
    $temp = $_GET["CorF"];
    $fahrenheit = (($degrees * 1.8 + 32);
    $celsius = (($degrees - 32) / 1.8;
    if ($temp == "a") echo "The temperature in Fahrenheit is ", $fahrenheit;
    if ($temp == "b") echo "The temperature in Celsius is ", $celsius;
}
?>

<HTML>
<BODY>
<form action="m2.php" method"get">
<input type="text" name"degrees" />
<input type="radio" name="CorF" value="a" />
   °C
<input type="radio" name="CorF" value="b" />
   °F
<input type="submit" name="submit" />
</form>
</BODY>
</html>

 

Ken

In addition to what Ken said, the reason your form is not working is that because the form is not properly submitting the GET variable of degrees.

 

<input type="text" name"degrees" />

 

change that to

 

<input type="text" name="degrees" />

 

You (are) were missing an equals sign. Thus, since $_GET[degrees] was always zero, that will explain why oF was always 32.

 

Regards,

Ironphp

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.