php:表单无法正确评估

时间:2014-04-09 01:09:23

标签: php forms select

Newb在这里尝试制作一个简单的温度转换器,在其中输入数据,然后选择过滤器来处理日期。我以前从未使用过选择,并且无法弄清楚如何处理数据以仅显示所选值。现在,如果你们ferinheight和celcius都将打印到屏幕上。我只想打印下拉/选择中选择的值。

<?php
    if (isset ($_POST['submit'])){//data is subnmitted, show it
        //$myTemp = (int)$_POST['myTemp'];// make it an int
        $myTemp = (float)$_POST['myTemp'];// float more forving

        $F = $myTemp  *  9/5 + 32;
        $C = $myTemp  *  9/5 - 32;

        if ($myFilter == 'f'){echo $F . "&ordm; ferinheight";}
        if ($myFilter == 'c'){echo $C . "&ordm; Celcius ";}

    }else{
?>
<form action="02temp.php" method="post">
    Enter Value to convert: <input type="text" name="myTemp" />
    <br />
    Select data filter:
    <select name="myFilter">
      <option value="f">F&ordm;</option>
      <option value="c">C&ordm;</option>
    </select>
    <br /><br />
    <input type="submit" name="submit" /> <!--purposely blurred -->
</form>
<?php
     }
?>

1 个答案:

答案 0 :(得分:3)

你有几个错误:

1)您忘记为$myFilter分配一个值。

 $myFilter = $_POST['myFilter']; // <-- You forgot this

2)但即使你这样做,你也可以在你的if语句中使用赋值运算符(=)而不是比较运算符(==),使它们都成立:

if ($myFilter = 'f'){echo $F . "&ordm; ferinheight";}
if ($myFilter = 'c'){echo $C . "&ordm; Celcius ";}

应该是

if ($myFilter == 'f'){echo $F . "&ordm; ferinheight";}
if ($myFilter == 'c'){echo $C . "&ordm; Celcius ";}