Home > Enterprise >  Can someone help me to solve my echo problem for my temperature converter PHP
Can someone help me to solve my echo problem for my temperature converter PHP

Time:02-08

So here is my code for temperature conversion in PHP

<html>
<head> 
      <title>Temp Conversion</title>
      <meta charset="utf-8">
</head>
<body>
    <form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">

        <table>
        <tr>
            <td>Enter value to convert</td>
            <td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
        </tr>

        <tr>
            <td>Convert from:</td>
            <td><select name="convertFrom" id="convertFrom" size="1">
                       <option disabled> Select a measurement type</option>
                       <option value="celsiusF">Celsius</option>
                       <option value="fahrenheitF">Fahrenheit</option>
                       <option value="kelvinF">Kelvin</option>
                </select>
            </td>
        </tr>
        
        <tr>
            <td>Convert to:</td>
            <td><select name="convertType" id="convertType" size="1">
                       <option disabled> Select a measurement type</option>
                       <option value="celsius">Celsius</option>
                       <option value="fahrenheit">Fahrenheit</option>
                       <option value="kelvin">Kelvin</option>
                </select>
            </td>
        </tr>

        <tr>
            <td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
            <td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
        </tr>



    </form>

    <?php
        function tempConvert($valueConvert, $convertFrom, $convertType)
        {
            if($convertFrom== "celciusF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = (((9/5)*$valueConvert)  (32));
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = ($valueConvert);
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ($valueConvert   273);
                }
            
            }
            
            else if($convertFrom== "fahrenheitF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = ($valueConvert);
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = (($valueConvert - 32) * (5/9));
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ((($valueConvert - 32) * (5/9))   273);
                }
            
            }
            
            else if($convertFrom== "kelvinF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = (((9/5)*($valueConvert - 273))  (32));
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = ($valueConvert - 273);
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ($valueConvert);
                }
            
            }
            
        
        }

        if (isset($_POST['btnConvert'])) 
        { 
            $valueConvert = $_POST['valueConvert'];
            $convertFrom = $_POST['convertFrom'];
            $convertType = $_POST['convertType'];
            $conversion = tempConvert($valueConvert, $convertFrom, $convertType);
            
            echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
        }
    ?>

    </body>
</html>

When I run this PHP code, then inserting the value, and pressing the convert button. It seems that the $conversion value does not shown in the echo at the last part of the code. (for example it will only be shown like "The initial temperature was 32. The new temperature is celsius")

I wonder what caused the problem, can anyone help me?

CodePudding user response:

Ok, thanks to Nigel Ren, ADyson, and Barmar answer, it seems that I have 2 problem in my code, the first is the lack of return value, and the second is a typo when typing "celciusF" (it should be celsiusF, using s instead of c)

Here is the correct code

<html>
<head> 
      <title>Temp Conversion</title>
      <meta charset="utf-8">
</head>
<body>
    <form name="tempConvert" method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">

        <table>
        <tr>
            <td>Enter value to convert</td>
            <td><input type="text" name="valueConvert" id="valueConvert" size="15"></td>
        </tr>

        <tr>
            <td>Convert from:</td>
            <td><select name="convertFrom" id="convertFrom" size="1">
                       <option disabled> Select a measurement type</option>
                       <option value="celsiusF">Celsius</option>
                       <option value="fahrenheitF">Fahrenheit</option>
                       <option value="kelvinF">Kelvin</option>
                </select>
            </td>
        </tr>
        
        <tr>
            <td>Convert to:</td>
            <td><select name="convertType" id="convertType" size="1">
                       <option disabled> Select a measurement type</option>
                       <option value="celsius">Celsius</option>
                       <option value="fahrenheit">Fahrenheit</option>
                       <option value="kelvin">Kelvin</option>
                </select>
            </td>
        </tr>

        <tr>
            <td><input type="submit" name="btnConvert" id="btnConvert" value="Convert"></td>
            <td><input type="reset" name="btnReset" id="btnReset" value="Reset"></td>
        </tr>



    </form>

    <?php
        function tempConvert($valueConvert, $convertFrom, $convertType)
        {
            if($convertFrom== "celsiusF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = (((9/5)*$valueConvert)  (32));
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = ($valueConvert);
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ($valueConvert   273);
                }
            
            }
            
            else if($convertFrom== "fahrenheitF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = ($valueConvert);
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = (($valueConvert - 32) * (5/9));
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ((($valueConvert - 32) * (5/9))   273);
                }
            
            }
            
            else if($convertFrom== "kelvinF")
            {
                if($convertType== "fahrenheit")
                {
                   $conversion = (((9/5)*($valueConvert - 273))  (32));
                }
                
                else if ($convertType== "celsius")
                {
                   $conversion = ($valueConvert - 273);
                }
                
                else if ($convertType== "kelvin")
                {
                   $conversion = ($valueConvert);
                }
            
            }
            
        return $conversion;
        }

        if (isset($_POST['btnConvert'])) 
        { 
            $valueConvert = $_POST['valueConvert'];
            $convertFrom = $_POST['convertFrom'];
            $convertType = $_POST['convertType'];
            $conversion = tempConvert($valueConvert, $convertFrom, $convertType);
            
            echo "The initial temperature was $valueConvert. The new temperature is $conversion $convertType.";
        }
    ?>

    </body>
</html>
  •  Tags:  
  • Related