-1

I am trying to make a temperature converter using a form and PHP and using SWITCH case. I believe I am close to a solution, however when I hit the submit button, my page goes blank and I am not seeing my echo statement. Here is my code:

<?php

if(isset($_POST['convertTemp']))

{

switch('$convertTemp'){

    case 'FtoC':
        $newTemp = (($tEmp - 32)* (5/9));
        break;

    case 'FtoK':
       $newTemp = (($tEmp - 32)* (5/9) + 273.15);
        break;

    case 'KtoF':
       $newTemp = (($tEmp - 273.15)* (9/5) + 32);
        break;

     case 'KtoC':
       $newTemp = ($tEmp - 273.15);
        break;

    case 'CtoK':
       $newTemp = ($tEmp + 273.15);
        break;

    case 'CtoF':
       $newTemp = (($tEmp * 9/5) + 32);
        break;      
  echo " <h2 align='center'>The initial temperature was" . $tEmp . "and the converted temperature is:" . $newTemp . "/h2>";    

}
}else{
    echo'




<html>
<body>

<h1 align="center">Convert a Temperature</h1> 

    <form align="center" method="POST">

        Enter the tempurature you wish to convert:<input type="number" name="tEmp">

        <h2>Convert temperature from: </h2>
        <input type="radio" name="convertTemp" value="FtoC"> Farenheit to Celcius <br>
        <input type="radio" name="convertTemp" value="FtoK"> Farenheit to Kelvin <br>
        <input type="radio" name="convertTemp" value="KtoF"> Kelvin to Farenheit <br>
        <input type="radio" name="convertTemp" value="KtoC"> Kelvin to Celcius <br>
        <input type="radio" name="convertTemp" value="CtoK"> Celcius to Kelvin <br>
        <input type="radio" name="convertTemp" value="CtoF"> Celcius to Farenheit <br>

    <input type="submit" value="Convert Tempurature!">

    </form>



    </body>
    ';

    }

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
YanaWhite
  • 3
  • 1
  • `switch('$convertTemp'){` should be `switch($_POST['convertTemp']){` – Nick Jan 09 '19 at 23:18
  • @Nick, I don't think this is a duplicate - the quotes didn't cause an issue (they properly used double quotes within single quotes - and yes, the variable was surrounded by single quotes, but that's not what caused the echo to fail - as was their original question). –  Jan 09 '19 at 23:38
  • @mark.hch you are right about the `echo` needing to be outside the `switch` block but more importantly the switch block will never produce a result as the value of `$convertTemp` will not be substituted inside single quotes. – Nick Jan 09 '19 at 23:56
  • @Nick I understand that - but the question relates to why the screen is blank, and why the echo is not appearing. The value won't be accurate unless they remove the single-quotes, but that's not causing the issue they were mentioning. If we were able to clear up the `echo`'s location issue, and the script still failed, and they posted a new question stating "The switch is not producing a result" or similar, then I would agree it's a duplicate. However, if this were marked as dupe before I answered, then I would have to clear up the actual issue (blank page) in the comments. –  Jan 10 '19 at 00:28

1 Answers1

0

There were a couple issues here. The echo you weren't seeing was in the switch statement after the last break, when it needed to come after the switch (i.e. after the closing bracket of the switch statement). However, even doing that, the variables aren't set ($tEmp is not set, you need to get its value from $_POST first, same as $convertTemp).

<?php
  if(isset($_POST['convertTemp']) && isset($_POST['tEmp'])) {
    $convertTemp = $_POST['convertTemp'];
    $tEmp = $_POST['tEmp'];
    switch($convertTemp){
        case 'FtoC':
            $newTemp = (($tEmp - 32)* (5/9));
            break;
        case 'FtoK':
           $newTemp = (($tEmp - 32)* (5/9) + 273.15);
            break;
        case 'KtoF':
           $newTemp = (($tEmp - 273.15)* (9/5) + 32);
            break;
         case 'KtoC':
           $newTemp = ($tEmp - 273.15);
            break;
        case 'CtoK':
           $newTemp = ($tEmp + 273.15);
            break;
        case 'CtoF':
           $newTemp = (($tEmp * 9/5) + 32);
            break;      
    }
    echo "<h2 align='center'>The initial temperature was " . $tEmp . " and the converted temperature is: " . $newTemp . "</h2>";
  }
  else {
    echo'
      <html>
        <body>
          <h1 align="center">Convert a Temperature</h1>
          <form align="center" method="POST">
            Enter the tempurature you wish to convert:<input type="number" name="tEmp">

            <h2>Convert temperature from: </h2>

            <input type="radio" name="convertTemp" value="FtoC"> Farenheit to Celcius <br>
            <input type="radio" name="convertTemp" value="FtoK"> Farenheit to Kelvin <br>
            <input type="radio" name="convertTemp" value="KtoF"> Kelvin to Farenheit <br>
            <input type="radio" name="convertTemp" value="KtoC"> Kelvin to Celcius <br>
            <input type="radio" name="convertTemp" value="CtoK"> Celcius to Kelvin <br>
            <input type="radio" name="convertTemp" value="CtoF"> Celcius to Farenheit <br>

            <input type="submit" value="Convert Tempurature!">
          </form>
        </body>
      </html>
    ';
  }
?>
  • Thank you so much!! This is exactly what I was looking for. – YanaWhite Jan 09 '19 at 23:28
  • 1
    Happy to help! If it solved your issue, be sure to accept it as the answer, so people can find it, and so no one else wastes time solving a solved issue. Have a great day! –  Jan 09 '19 at 23:33