dongyuelian9602 2014-11-01 23:10
浏览 52
已采纳

温度转换PHP

I have been working on the following simple temperature conversion using HTML and PHP. It is my first attempt at using PHP and I can't seem to get it right.

I am sure there are lots of errors in my code so please be patient!

Here is what I have:

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

I can't figure out how to pass the users textbox input and dropdown list selection to the php function.

  • 写回答

2条回答 默认 最新

  • dongqiaogouk86049 2014-11-01 23:18
    关注

    You pretty much have things set up alright. You are not calling the function though.

    You echo statement comes after the return statement and will never be executed.

    It would be better to do something like this:

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

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?