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 . "º ferinheight";}
if ($myFilter == 'c'){echo $C . "º 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º</option>
<option value="c">Cº</option>
</select>
<br /><br />
<input type="submit" name="submit" /> <!--purposely blurred -->
</form>
<?php
}
?>
答案 0 :(得分:3)
你有几个错误:
1)您忘记为$myFilter
分配一个值。
$myFilter = $_POST['myFilter']; // <-- You forgot this
2)但即使你这样做,你也可以在你的if语句中使用赋值运算符(=
)而不是比较运算符(==
),使它们都成立:
if ($myFilter = 'f'){echo $F . "º ferinheight";}
if ($myFilter = 'c'){echo $C . "º Celcius ";}
应该是
if ($myFilter == 'f'){echo $F . "º ferinheight";}
if ($myFilter == 'c'){echo $C . "º Celcius ";}