使用开关和开机自检的PHP温度转换器表单在点击提交后出现空白



我正在尝试使用表单和PHP并使用SWITCH大小写来制作温度转换器。我相信我已经接近解决方案,但是当我点击提交按钮时,我的页面变为空白,我没有看到我的 echo 语句。 这是我的代码:

<?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>
    ';
    }
?>

这里有几个问题。您没有看到的echo是在最后一个break之后的switch语句中,当它需要在开关之后(即在switch语句的右括号之后)。但是,即使这样做,也不会设置变量($tEmp未设置,您需要首先从$_POST获取其值,与$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>
    ';
  }
?>

最新更新