2017-03-09 133 views
-3

我是PHP新手,我正在嘗試編寫兩個函數,將攝氏溫度轉換爲華氏溫度,反之亦然,溫度是用戶輸入併發布了轉換,下面是我的我希望能得到幫助,在PHP中將攝氏溫度轉換爲華氏溫標和副版本

代碼預先感謝您向任何人誰可以幫助

<?php 
function fahrenheit_to_celsius($temp) 
{ 
    $celsius=5/9*($temp-32); 
    return $celsius ; 
} 

function celsius_to_fahrenheit($temp) 
{ 
    $fahrenheit=$temp*9/5+32; 
    return $fahrenheit ; 
} 



?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Change Temperature</title> 
     <style> 
      #myForm { 
    border: 1px solid grey; 
    padding: 5px; 
    width: 600px; 
} 

.centered { 
    width: 100%; 
    text-align: center; 
} 


     </style> 
    </head> 
    <body> 
    <div id="myForm"> 
     <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 


      <label for="temperature">Temperature:</label> 
      <select id="temperature" name="temperature" class="myDropdown"> 
       <option value="">Please select a temperature...</option> 
       <option value="C">celsius to fahrenheit</option> 
       <option value="F">fahrenheit to celsius</option> 
      </select> 

      <br><br> 



      <br><br> 

      <?php 
      if ($temperature==="C"){ 

        echo celsius_to_fahrenheit($_POST[$celsius]); 
       } 


      if ($teperature==="F"){ 
       echo fahrenheit_to_celsius($_POST[$temp]); 

      } 

      ?> 

      <label for="temp">Enter your temperature:</label> 
      <input type="text" id="temp" name="temp"> 




      <div class="centered"> 
       <input type="submit" value="Submit temperature"> 
      </div> 

     </form> 
    </div> 

    </body> 
</html> 
+0

'$ temperature',也不' $ teperature' ar從未設置。 – chris85

+1

[PHP:「注意:未定義的變量」,「注意:未定義的索引」和「注意:未定義的偏移量」的可能的重複](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-注意,未定義指數和通知書,基金) – chris85

回答

0

這是工作愛迪如何解決:

<?php 

$conversion = $_GET["conversion"]; 
echo $conversion; 
$temp = $_GET["ForC"]; 
$celsius=5/9*($temp-32); 
$fahrenheit=$temp*9/5+32; 



$dbh = new PDO('mysql:host=localhost;dbname=populatedropdown', "root", ""); 

$query = $dbh->query("select * from position"); // Run your query 

echo '<form action="tempcfc.php" method="get">'; 

echo "conversion<br>"; 
echo '<select name="conversion">'; // Open your drop down box 



// Loop through the query results, outputing the options one by one 
while ($row = $query->fetch(PDO::FETCH_ASSOC)) { 
    echo '<option value="'.$row['id'].'">'.$row['name'].'</option>'; 
} 

echo '</select>';// Close your drop down box 
echo "<br>"; 
echo "Type C or F<br>"; 
echo '<input type="text" name="ForC">'; 
echo "<br>"; 
echo "F<br>"; 
echo "<input type='text' name='F' value='" . $fahrenheit . "'>"; 
echo "<br>"; 
echo "C<br>"; 
echo "<input type='text' name='C' value='" . $celsius . "'>"; 
echo "<br>"; 
echo '<input type="submit" value="Submit">'; 

echo '</form>'; 

if ($conversion = "1") { 
    $query = $dbh->query("insert into position (temp) values ('$fahrenheit')"); 
} 
else { 
    $query = $dbh->query("insert into position (temp) values ('$celsius')"); 
} 


?> 
相關問題