首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用HTML表单和PHP函数进行温度转换

使用HTML表单和PHP函数进行温度转换
EN

Stack Overflow用户
提问于 2018-08-02 20:54:50
回答 2查看 1.2K关注 0票数 2

我正在尝试转换deg -> c和c -> deg使用php。目标是使用包含公式的函数,然后拥有一个带有单选按钮和文本框的表单。用户应该能够在文本框中输入学位,单击他们选择的单选按钮(F或C),然后提交表单以接收他们的转换。我见过类似的帖子,但方法并不针对我遇到的问题。

我已经更新了我的代码,但现在得到了“死亡白页”,有人能看到我看不到的错误吗?谢谢!

HTML

代码语言:javascript
复制
<h1>Temperature Conversion</h1>



<form action='lab_exercise_6.php' method ='POST'>
    <p>Enter a temp to be converted and then choose the conversion type below.</p>

<input type ='text' maxlength='3' name ='calculate'/>

 <p>Farenheit
  <input type="radio" name='convertTo' value="f" />
</p>

<p>Celsius
  <input type="radio" name='convertTo' value="c" />
</p>

<input type='submit' value='Convert Temperature' name='convertTo'/>

PHP

代码语言:javascript
复制
<?php

//function 1

function FtoC($deg_f) {
    return ($deg_f - 32) * 5 / 9;
  }

  //function 2
function CtoF($deg_c){
    return($deg_c + 32) * 9/5;
}

if( isset($_POST['convertTo']) && $_POST['convertTo'] ==="c" ){
$farenheit = FtoC($deg_f);
print('This temperature in FARENHEIT is equal to ' . $celsius . ' degrees celsius! </br>');
}else if(isset($_POST['convertTo'])&& $_POST['convertTo']==='f'){
    $celsius = CtoF($deg_c);
    print('This temperature in CELSIUS is equal to ' . $farenheit . ' degrees farenheit! </br>');

}

?>
EN

回答 2

Stack Overflow用户

发布于 2018-08-02 21:01:48

您尚未向单选框添加值。

然后将您的单选名称更改为与另一个相同,例如

代码语言:javascript
复制
<p>Farenheit
    <input type = 'radio' name ='temp_type' value='f'/>
</p>

<p>Celsius
    <input type = 'radio' name ='temp_type' value='c'/>
</p>

现在你可以用PHP来访问它们了。

代码语言:javascript
复制
if($_POST['temp_type'] && ($_POST['calculate']){
  $temp2calculate = $_POST['calculate'];
  $temp_type = $_POST['temp_type'];
}
票数 1
EN

Stack Overflow用户

发布于 2018-08-02 21:02:22

您必须为单选按钮指定相同的名称

代码语言:javascript
复制
    <p>Farenheit
        <input type = 'radio' value ='farenheit' name='units'/>
    </p>

    <p>Celsius
        <input type = 'radio' value ='celsius' name='units'/>
    </p>

检查是否选中了哪个单选按钮:

代码语言:javascript
复制
<?php
$units = $_POST['units'];
if($units == "fahrenheit"){
 //call function to convert to fahrenheit
}
else{
  //call function to convert to celsius
}
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51654227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档