我正在尝试转换deg -> c和c -> deg使用php。目标是使用包含公式的函数,然后拥有一个带有单选按钮和文本框的表单。用户应该能够在文本框中输入学位,单击他们选择的单选按钮(F或C),然后提交表单以接收他们的转换。我见过类似的帖子,但方法并不针对我遇到的问题。
我已经更新了我的代码,但现在得到了“死亡白页”,有人能看到我看不到的错误吗?谢谢!
HTML
<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
<?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>');
}
?>
发布于 2018-08-02 21:01:48
您尚未向单选框添加值。
然后将您的单选名称更改为与另一个相同,例如
<p>Farenheit
<input type = 'radio' name ='temp_type' value='f'/>
</p>
<p>Celsius
<input type = 'radio' name ='temp_type' value='c'/>
</p>
现在你可以用PHP来访问它们了。
if($_POST['temp_type'] && ($_POST['calculate']){
$temp2calculate = $_POST['calculate'];
$temp_type = $_POST['temp_type'];
}
发布于 2018-08-02 21:02:22
您必须为单选按钮指定相同的名称
<p>Farenheit
<input type = 'radio' value ='farenheit' name='units'/>
</p>
<p>Celsius
<input type = 'radio' value ='celsius' name='units'/>
</p>
检查是否选中了哪个单选按钮:
<?php
$units = $_POST['units'];
if($units == "fahrenheit"){
//call function to convert to fahrenheit
}
else{
//call function to convert to celsius
}
?>
https://stackoverflow.com/questions/51654227
复制相似问题