温度转换PHP

问题描述:

我一直在使用HTML和PHP进行以下简单的温度转换。这是我第一次尝试使用PHP,我似乎无法正确使用它。温度转换PHP

我相信我的代码有很多错误,请耐心等待!

以下是我有:

<!DOCTYPE HTML> 

<html> 
<head> 
     <title>Temp Conversion</title> 
     <meta charset="utf-8"> 
<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 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> 
     </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 


$valueConvert = $_POST['valueConvert']; 
$convertType = $_POST['convertType']; 
function tempConvert($valueConvert, $convertType){ 
    if($convertType == "fahrenheit"){ 
     $conversion = ((9/5)*$valueConvert) +(32); 
    } 
    else if ($convertType == "celsius"){ 
     $conversion = ($valueConvert - 32) * (9/5); 
    } 
return $conversion; 
echo "The initial temperature was $valueConvert. The new temperature is $conversion."; 
} 
?> 

    </body> 
</html> 

我无法弄清楚如何将用户输入文本框和下拉列表选择传递给PHP函数。

+1

首先您需要关闭“头“ta g,其次,你需要检查提交按钮是否被点击'if(isset($ _ POST ['btnConvert'])){// do something}' 你试图创建的函数,放置函数括号外的回显.. – Tommy 2014-11-01 23:12:22

+0

如果页面只是发回自己,您也可以将'action'属性留作空白值(所以,'action =“”')。 – 2014-11-01 23:16:36

+0

当你试图学习时,你应该自己测试函数/方法而不会引入不必要的复杂性。像这样:http://codepad.org/psEywckw正如你所看到的,华氏温度到摄氏温度计算不正确。 ([*咳咳*](http://codepad.org/9vBUGeLE)) – 2014-11-01 23:18:36

你几乎有东西设置好了。你不是在调用这个函数。

你在echo语句后附带回显语句,并且永远不会执行。

这将是更好的做这样的事情:

<?php 

function tempConvert($valueConvert, $convertType) 
{ 
    if($convertType == "fahrenheit"){ 
     $conversion = ((9/5) * $valueConvert) + (32); 
    } 
    else if ($convertType == "celsius"){ 
     $conversion = ($valueConvert - 32) * (5/9); 
    } 
    return $conversion; 
} 

$valueConvert = $_POST['valueConvert']; 
$convertType = $_POST['convertType']; 
$conversion = tempConvert($valueConvert, $convertType); 
echo "The initial temperature was $valueConvert. The new temperature is $conversion."; 

?> 

首先,我在你忘了取消head标签的评论中写道,和第二,所有你需要的检查,如果转换按钮被关闭,我固定的转换功能藏汉,不知道它是如何工作的好,我不是在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 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> 
     </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($value, $type){ 
    if($type== "fahrenheit"){ 
     return (((9/5)*$value) +(32)); 
    } 
    elseif ($type== "celsius"){ 
     return (($valueConvert - 32) * (9/5)); 
    } 
} 

if (isset($_POST['btnConvert'])) { 
$valueConvert = $_POST['valueConvert']; 
$convertType = $_POST['convertType']; 

echo "The initial temperature was $valueConvert. The new temperature is tempConvert($valueConvert, $convertType)."; 
} 
?> 

    </body> 
</html>