2017-07-31 79 views
1

我努力讓我的PHP單位轉換器工作。單位轉換器PHP

我想讓多個轉換器,但似乎無法讓它工作。

這將會是巨大的,如果有人可以告訴我,我要去哪裏錯了,幫我弄下去。 :)

<?php 
if($_POST){ 
    $fahrenheit = $_POST['fahrenheit']; 
    $celsius = ($fahrenheit - 32)*5/9; 
} 

if($_POST){ 
    $celsius = $_POST['celcius']; 
    $fahrenheit = ($celcius - 32)*5/9; 
} 
?> 


     <form action="" method="post"> 
     Fahrenheit: <input type="text" name="fahrenheit" /><br /> 
     <?php 
     if(isset($celsius)){ 
      echo "Celsius = ".$celsius; 
     } 
     ?> 
     </form> 

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

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

    function inches_to_centimeter($given_value) 
    { 
     $centimeter=$given_value/2.54; 
     return $centimeter ; 
    } 

    function centimeter_to_inches($given_value) 
    { 
     $inches=$given_value*2.54 
    } 
?> 

<html> 
    <head> 
     <title>Temp. Conv.</title> 
    </head> 
    <body> 
     <form action="" method="post"> 
     <table> 
<!-- FAHRENHEIGHT & CELCIUS V --> 
      <tr> 
       <td> 
        <select name="first_temp_type_name"> 
         <option value="fahrenheit">Fahrenheit</option> 
         <option value="celsius">Celsius</option> 
        </select> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" name="given_value"> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <select name="second_temp_type_name"> 
         <option value="fahrenheit">Fahrenheit</option> 
         <option value="celsius">Celsius</option> 
        </select> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="submit" name="btn" value="Convert"> 
       </td> 
      </tr> 

<!--FAHRENHEIGHT & CELCIUS^--> 

<!-- CENTEMETERS & INCHES --> 

      <tr> 
       <td> 
        <select name="first_length_type_name"> 
         <option value="centimeter">centimeter</option> 
         <option value="inches">Inches</option> 
        </select> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" name="given_value"> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        <select name="second_length_type_name"> 
         <option value="centimeter">centimeter</option> 
         <option value="inches">Inches</option> 
        </select> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" name="given_value"> 
       </td> 
      </tr> 

<!--CENTEMETERS & INCHES ^--> 

      <tr> 
       <td> 
        <?php 
        if(isset($_POST['btn'])) 
        { 
         $first_temp_type_name=$_POST['first_temp_type_name']; 
         $second_temp_type_name=$_POST['second_temp_type_name']; 
         $given_value=$_POST['given_value']; 
         if($first_temp_type_name=='fahrenheit') 
         { 
          $celsious=fahrenheit_to_celsius($given_value); 
          echo "Fahrenheit $given_value = $celsious Celsious"; 
         } 
         if($first_temp_type_name=='celsius') 
         { 
          $fahrenheit=celsius_to_fahrenheit($given_value); 
          echo "Celsious $given_value = $fahrenheit Fahrenheit"; 
         } 


        } 

        if(isset($_POST['btn'])) 
        { 
         $first_length_type_name=$_POST['first_length_type_name']; 
         $second_length_type_name=$_POST['second_length_type_name']; 
         $given_value=$_POST['given_value']; 
         if($first_length_type_name=='centimeter') 
         { 
          $centimeter=centimeter_to_inches($given_value); 
          echo "Centimeter $given_value = $inches Inches"; 
         } 
         if($first_length_type_name=='inches') 
         { 
          $centimeter=inches_to_centimeter($given_value); 
          echo "Inches $given_value = $centimeter centimeter"; 
         } 


        } 

        ?> 
       </td> 
      </tr> 
     </table> 
     </form> 
    </body> 
</html> 

我知道有很多事情,我的道歉。

任何幫助是極大的讚賞:)

+0

'如果($ _ POST){'需要數組鍵檢查 – Scuzzy

+0

我怎麼能去這樣做,對不起,我是新來的PHP。 –

+0

'如果($ _ POST)'是有效的@Scuzzy,它只檢查什麼(都)與POST方法 – Qirel

回答

0

嗨請使用下面的代碼。

<?php 

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

$convertedTemperature = 0; 

/* The condition is entered when the request is of POST type. */ 
if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    $temperature   = $_POST['temperature']; 
    $fromConvertionUnit  = $_POST['fromConvertionUnit']; 
    $toConvertionUnit  = $_POST['toConvertionUnit']; 

/* if the temperature conversion are of same unit then no need for conversion */ 

    if($fromConvertionUnit == $toConvertionUnit){ 
     $convertedTemperature = $temperature; 
    }else if($fromConvertionUnit == 'fahrenheit' && $toConvertionUnit == 'celcius') { 

/* formula to convert Fahrenheit to Celcius */ 
     $convertedTemperature = ($temperature - 32) * 0.5556; 
    }else if($fromConvertionUnit == 'celcius' && $toConvertionUnit == 'fahrenheit') { 

/* formula to convert Celcius to Fahrenheit */ 
     $convertedTemperature = ($temperature * 1.8) + 32; 
    } 
} 
?> 
<!doctype html> 
<html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Temperature Converter</title> 
    </head> 
    <body> 
     <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
      <div> 
<!-- If the temperature value has be submitted and has data then it will prefilled --> 
       <label for="temperature">Temperature</label> <br/> 
       <input type="number" name="temperature" id="temperature" value="<?php echo (!empty($temperature)) ? $temperature : '' ?>"> 
      </div> 
      <div> 
       <label for="fromConvertionUnit">Select From Convertion Unit</label><br/> 
       <select name="fromConvertionUnit" id="fromConvertionUnit"> 
        <option value="fahrenheit">Fahrenheit</option> 
        <option value="celcius">Celcius</option> 
       </select> 
     </div> 
     <div> 
       <label for="toConvertionUnit">Select To Convertion Unit</label><br/> 
       <select name="toConvertionUnit" id="toConvertionUnit"> 
        <option value="fahrenheit">Fahrenheit</option> 
        <option value="celcius">Celcius</option> 
       </select> 
     </div> 
<!-- Once the page is submitted and conversion is done the respective values will be added in the following section --> 
      <div> 
     Converted Temperature <b> <?php echo (!empty($temperature)) ? $temperature : '--' ?></b> Value From <b><?php echo (!empty($fromConvertionUnit)) ? $fromConvertionUnit : '--' ?></b> TO <b><?php echo (!empty($toConvertionUnit)) ? $toConvertionUnit : '--' ?></b> is <b><?php echo (!empty($convertedTemperature)) ? $convertedTemperature : '--' ?><b/> 
       <label for="converted_value">Converted Value</label><br/> 
<input type="number" value="<?php echo (!empty($convertedTemperature)) ? $convertedTemperature : '0' ?>"> 
      </div> 
      <div> 
<input type="submit" value="Convert"> 
      </div> 
     </form> 
    </body> 
</html> 

enter image description here

+0

爲什麼有人「複製和粘貼」呢?什麼使它工作,你改變了什麼?一個好的答案總是包含一些文字和描述。 – Qirel

+0

@Qirel我在我的代碼中添加了一條評論。首先,他添加了2次條件,這是不必要的,公式沒有適當地應用。在行動中,他沒有提到要重定向哪個頁面,因此必須通過使用此代碼來正確處理:<?php echo $ _SERVER ['PHP_SELF']?>。所有這一切,我曾說過他使用我的代碼。所以我希望能回答你的問題。你能否重新投票我的答案。 –

0

如果你想使一個轉換器,你需要的是一個輸入 - 你想轉換一下之間的下拉列表。然後在提交表單以檢查您希望使用的函數時,在PHP中使用switch

您當前的問題是,你重寫了很多價值,而且你不檢查正確的按鈕。這也過度複雜化了。通過使用filter_var()有一個標誌,是適合您的需求 -

您還可以通過確保輸入的是一個實際的數量發展這個轉換器。您可以驗證,或消毒它,看到FILTER_SANITIZE vs FILTER VALIDATE, whats the difference - and which to use?

<?php 
if (isset($_POST['submit'])) { 
    switch ($_POST['convert']) { 
     case "cm-in": 
      $result = centimeter_to_inches($_POST['value']); 
      $old_unit = 'cm'; 
      $new_unit = ' inches'; 
      break; 
     case "in-cm": 
      $result = inches_to_centimeter($_POST['value']); 
      $old_unit = ' inches'; 
      $new_unit = 'cm'; 
      break; 
     case "f-c": 
      $result = fahrenheit_to_celsius($_POST['value']); 
      $old_unit = ' Farenheit'; 
      $new_unit = ' Celcius'; 
      break; 
     case "c-f": 
      $result = celsius_to_fahrenheit($_POST['value']); 
      $old_unit = ' Celcius'; 
      $new_unit = ' Farenheit'; 
      break; 
    } 
} 

function fahrenheit_to_celsius($given_value) { 
    return 5/9*($given_value-32); 
} 

function celsius_to_fahrenheit($given_value) { 
    return $given_value*9/5+32; 
} 

function inches_to_centimeter($given_value) { 
    return $given_value/2.54; 
} 

function centimeter_to_inches($given_value) { 
    return $given_value*2.54; 
} 
?> 


<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Conversions</title> 
    </head> 
    <body> 
     <form method="post"> 
      Convert <input type="text" name="value" /><br /> 
      <select name="convert"> 
       <option value="">--Select one--</option> 
       <optgroup label="Units of Length"> 
        <option value="cm-in">Centimeter to inches</option> 
        <option value="in-cm">Inches to centimeter</option> 
       </optgroup> 

       <optgroup label="Units of Temperature"> 
        <option value="f-c">Farenheit to Celcius</option> 
        <option value="c-f">Celcius to Farenheit</option> 
       </optgroup> 
      </select> 
      <input type="submit" name="submit" value="Convert" /> 
     </form> 

     <?php 
     if (!empty($result)) 
      echo '<p>'.$_POST['value'].$old_unit.' equals '.$result.$new_unit.'</p>'; 
     ?> 
    </body> 
</html>