【发布时间】:2016-02-25 00:13:15
【问题描述】:
当我开始学习 PHP 时,我只是在做一个相当蹩脚的计算器。我不知道为什么一切似乎都很好,除了答案不会显示。
这是 HTML:
<head>
<meta charset="utf-8">
<title>A (Seriously) Simple Calculator</title>
<link rel="stylesheet" type="text/css" href="./calc_css.css">
</head>
<body>
<form method="post" attribute="post" action="calc1.php">
<p>First Value:<br/>
<input type="number" id="first" name="first" step="0.0000000001"></p>
<p>Second Value:<br/>
<input type="number" id="second" name="second" step="0.0000000001"></p>
<p>+<input type="radio" name="operation" id="add" value="add" checked="true"></p><br/>
<p>-<input type="radio" name="operation" id="subtract" value="subtract"></p><br/>
<p>X<input type="radio" name="operation" id="multiply" value="multiply"></p><br/>
<p>/<input type="radio" name="operation" id="divide" value="divide"></p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
这是我的 PHP:
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = floatval($_POST['first']);
$second = floatval($_POST['second']);
if($_POST['operation'] == 'add') {
echo $first + $second;
}
else if($_POST['operation'] == 'subtract') {
echo $first - $second;
}
else if($_POST['operation'] == 'multiply') {
echo $first * $second;
}
else($_POST['operation'] == 'divide') {
echo $first / $second;
}
?>
</p>
</body>
</html>
我认为这与我的输入步骤或类型无关,我已经在我的 PHP 文件中尝试了各种方式(我能想到的)。也就是说,我是一个非常绿色的初学者。任何帮助将不胜感激。
【问题讨论】:
-
您是在服务器上托管该 php 文件还是尝试在本地访问它(即从
file://url)? PHP 只能通过服务器运行。如果不是,则需要more details,而不仅仅是“我的代码不起作用”——您是否收到任何错误消息?
标签: php calculator