我正在尝试创建一个简单的计算器(这可能是更简单但不能找到错误:(叹气*),所以当我点击提交时,数字1和数字2将执行用户的操作但是答案在回复时没有回复:(有什么问题?非常感谢你!
<?php
if ($_POST['subBtn']) {
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
if($_POST['group1'] == 'add') {
echo $number1 + $number2 = $answer;
}
else if($_POST['group1'] == 'subtract') {
echo $number1 - $number2 = $answer;
}
else if($_POST['group1'] == 'times') {
echo $number1 * $num2ber = $answer;
}
else if($_POST['group1'] == 'divide') {
echo $number2 / $number2 = $answer;
echo "<p>" . $answer . " </p>";
}
}
?>
<form name="number1" action="activity-2-4.php" method="post">
number1:<select name="number1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>
number2:<select name="number2">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>
<input type="radio" name="group1" id="add" value="add">+<br/>
<input type="radio" name="group1" id="subtract" value="subtract">-<br/>
<input type="radio" name="group1" id="times" value="times">x<br/>
<input type="radio" name="group1" id="divide" value="divide">/<br/>
<button type="submit" name="subBtn" id="answer" value="answer">Calculate</button>
</form>
答案 0 :(得分:0)
不要在echo语句中为变量赋值。此外,赋值的变量应始终位于左侧。另外,你的回声是在分裂的最后一个区域内。
请改为尝试:
<?php
if (isset($_POST['subBtn']) && isset($_POST['group1'])) {
$number1 = isset($_POST['number1']) ? $_POST['number1'] : 0;
$number2 = isset($_POST['number2']) ? $_POST['number2'] : 0;
switch ($_POST['group1']) {
case 'add':
$answer = $number1 + $number2;
break;
case 'subtract':
$answer = $number1 - $number2;
break;
case 'times':
$answer = $number1 * $number2;
break;
case 'divide':
$answer = $number2 / $number2;
break;
default:
$answer = 'No operation method selected.';
break;
}
echo "<p>" . $answer . " </p>";
}
?>
答案 1 :(得分:0)
我相信这是你的问题:
echo $number2 / $number2 = $answer
将其更改为:
$answer = $number2 / $number2
每次数学运算都一样。
您尝试在存储数据的同时显示某些内容,但顺序错误。
在数学部分之后,你已经得到了答案的回声。
答案 2 :(得分:0)
您需要检查POST变量是否存在。
错误地使用了计算,变量和回声echo $number1 + $number2 = $answer;
我猜这种格式的预期输出4+1=5
。
<?php
if (isset($_POST['subBtn']))
{
$number1 = isset($_POST['number1']) ? $_POST['number1'] : '';
$number2 = isset($_POST['number2']) ? $_POST['number2'] : '';
$group1 = isset($_POST['group1']) ? $_POST['group1'] : '';
$answer = '';
$stmt = '';
switch ($group1)
{
case 'add':
$stmt = $number1.'+'.$number2.'= ';
$answer = $number1 + $number2;
break;
case 'subtract':
$stmt = $number1.'-'.$number2.'= ';
$answer = $number1 - $number2;
break;
case 'times':
$stmt = $number1.'*'.$number2.'= ';
$answer = $number1 * $number2;
break;
case 'divide':
$stmt = $number1.'/'.$number2.'= ';
$answer = $number1 / $number2;
break;
}
echo "<p>" . $stmt.$answer . " </p>";
}
?>
<form name="number1" action="activity-2-4.php" method="post">
number1:<select name="number1">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>
number2:<select name="number2">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select><br>
<input type="radio" name="group1" id="add" value="add">+<br/>
<input type="radio" name="group1" id="subtract" value="subtract">-<br/>
<input type="radio" name="group1" id="times" value="times">x<br/>
<input type="radio" name="group1" id="divide" value="divide">/<br/>
<button type="submit" name="subBtn" id="answer" value="answer">Calculate</button>
</form>
答案 3 :(得分:0)
$ _POST全局变量将所有内容存储为字符串。 从理论上讲,PHP会在后端键入杂乱并将其转换为数字,但这不会发生在你身上,我不知道你运行的是哪个版本的PHP。所以你可以明确地将它转换为你自己。 试试这个
$number1 = (float)$_POST['number1'];
$number2 = (float)$_POST['number2'];
如果你知道你得到一个int你可以做
$number1 = (int)$_POST['number1'];
$number2 = (int)$_POST['number2'];
但是我觉得你的问题出在group1。
print_r($_POST);
确保在那里有group1,并确保它的小写值完全相同,因为您以小写形式检查并且此操作区分大小写。
接下来的步骤是在屏幕上打印一些东西,并确保你在
开头echo "i'm in the add case";
等等
答案 4 :(得分:-1)
更改分配值的方式。变化
$number1 + $number2 = $answer;
到
$answer = $number1 + $number2;