【问题标题】:Unit Converter PHP单位转换器 PHP
【发布时间】:2018-01-07 00:00:54
【问题描述】:

我正在努力让我的 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>

我知道发生了很多事情,我很抱歉。

非常感谢任何帮助:)

【问题讨论】:

  • if($_POST){ 需要数组键检查
  • 我该怎么做,对不起,我是 PHP 新手。
  • if ($_POST) 是有效的@Scuzzy,它只是检查是否使用 POST 方法发送了任何内容
  • 在“San Google”上搜索,您会发现许多 (tons) 初学者教程,您可以在其中学习如何使用 PHP 制作表单:-)。
  • @Qirel 是的,但是 $fahrenheit$celsius 被两个 if post 语句覆盖。

标签: php html converter


【解决方案1】:

如果您想制作一个转换器,您只需要一个输入 - 以及您想要转换的内容之间的下拉菜单。然后在提交表单时在 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>

【讨论】:

    【解决方案2】:

    您好,请使用以下代码。

    <?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>
    

    【讨论】:

    • 为什么有人要“复制和粘贴”这个?是什么让它起作用,你改变了什么?一个好的答案总是包含一些文字和描述。
    • @Qirel 我在我的代码中添加了一个 cmets。首先,他添加了 2 次不必要的条件,并且公式没有正确应用。在行动中,他没有提到要重定向哪个页面,因此必须通过使用以下代码正确处理:。因此,我曾说过他要使用我的代码。所以我希望这能回答你的问题。你能重新投票给我的答案吗?
    猜你喜欢
    • 2013-09-04
    • 2015-04-06
    • 2016-08-14
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2014-12-27
    相关资源
    最近更新 更多