我一直在使用HTML和PHP进行以下简单的温度转换。这是我第一次尝试使用PHP而我似乎无法正确使用它。
我确信我的代码中有很多错误,所以请耐心等待!
这就是我所拥有的:
<!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>
我无法弄清楚如何将用户文本框输入和下拉列表选择传递给php函数。
答案 0 :(得分:7)
你几乎把事情搞定了。你不是在调用这个函数。
你的echo语句出现在return语句之后,永远不会被执行。
做这样的事情会更好:
<?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 :(得分:1)
首先,我在评论中写道,你忘了取消头标记,其次你需要检查转换按钮是否关闭,我修复你的转换功能,还不知道它的工作有多好,我不是专业的PHP,但希望它的工作原理:)
<html>
<head>
<title>Temp Conversion</title>
<meta charset="utf-8">
</head>
<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
function tempConvert($value, $type){
if($type== "fahrenheit"){
return (((9/5)*$value) +(32));
}
elseif ($type== "celsius"){
return (($valueConvert - 32) * (9/5));
}
}
if (isset($_POST['btnConvert'])) {
$valueConvert = $_POST['valueConvert'];
$convertType = $_POST['convertType'];
echo "The initial temperature was $valueConvert. The new temperature is tempConvert($valueConvert, $convertType).";
}
?>
</body>
</html>