我制作了一个计算器,但只有一个加法操作正在运行。
这是代码
<form>
<input type = "text" name = "num1"/>
<input type = "text" name = "num2"/>
<select name = "opr">
<option>Add</option>
<option>MULTIPLY</option>
<option>SUBTRACT</option>
<option>DIVIDE</option>
</select>
<br>
<button type ="submit" name ="submit" value = "submit">submit</button>
</form>
<?php
if(isset($_GET['submit']))
{
$num1 = $_GET["num1"];
$num2 = $_GET["num2"];
$opr = $_GET["opr"];
if($opr = "ADD")
{
$num3 = $num1+$num2;
echo $num3;
}
elseif($opr = "MULTIPLY" )
{
$num3 = $num1*$num2;
echo $num3;
}
elseif($opr = "SUBTRACT" )
{
$num3 = $num1-$num2;
echo $num3;
}
else{
$num3 = $num1/$num2;
echo $num3;
}
}
?>
我也为其他操作做了条件声明,但现在确定它为什么不起作用。我试图使用$ _GET函数从URL获取值。但只有ADD方法才有效。
答案 0 :(得分:0)
您正在使用=
代替==
来检查条件。
我已经更新了你的代码。请找到如下。
<form>
<input type = "text" name = "num1"/>
<input type = "text" name = "num2"/>
<select name = "opr">
<option>ADD</option>
<option>MULTIPLY</option>
<option>SUBTRACT</option>
<option>DIVIDE</option>
</select>
<br>
<button type ="submit" name ="submit" value = "submit">submit</button>
</form>
<?php
if(isset($_GET['submit']))
{
$num1 = $_GET["num1"];
$num2 = $_GET["num2"];
$opr = $_GET["opr"];
if($opr == "ADD")
{
$num3 = $num1+$num2;
echo $num3;
}
elseif($opr == "MULTIPLY" )
{
$num3 = $num1*$num2;
echo $num3;
}
elseif($opr == "SUBTRACT" )
{
$num3 = $num1-$num2;
echo $num3;
}
else{
$num3 = $num1/$num2;
echo $num3;
}
}
?>