2014-11-01 153 views
1

我一直在使用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

回答

7

你幾乎有東西設置好了。你不是在調用這個函數。

你在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."; 

?> 
1

首先,我在你忘了取消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>