我尝试尽可能多地重新格式化我的代码,但我没有向浏览器显示任何结果。如果你可以过来看,也许让我知道我哪里出错,那将是非常感谢。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>
Untitled Document
</title>
</head>
<body>
<form action="cal.php" method="$_POST">
<input type="number" name="value1" placeholder="Enter Number 1" />
<select name="operator">
<option value="add">+</option>
<option value="subtract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="number" name="value2" placeholder="Enter Number 2" />
<input type="submit" value="calculate" />
</form>
</body>
</html>
我的php
<html>
<body>
<?php
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$operator = $_POST["operator"];
if ($operator == "add"){
$answer = $value1 + $value2;
echo "<p>Your Answer is: $answer</p>";
}
if ($operator == "subtract"){
$answer = $value1 - $value2;
echo "<p>Your Answer is: $answer</p>";
}
if ($operator == "multiply"){
$answer = $value1 * $value2;
echo "<p>Your Answer is: $answer</p>";
}
if ($operator == "divide"){
$answer = $value1 / $value2;
echo "<p>Your Answer is: $answer</p>";
}
?>
</body>
</html>
我觉得我的代码是正确的,但不确定为什么它没有在浏览器中显示答案。
提前致谢!
答案 0 :(得分:0)
您在HTML中发布的方式不正确。
<form action="cal.php" method="$_POST">
将其更改为:
<form action="cal.php" method="POST">
答案 1 :(得分:0)
method = "POST"
而不是
method = "$_POST"
答案 2 :(得分:0)
您只需将表单的方法更改为&#34; POST&#34;
你的方法是
<form action="cal.php" method="$_POST">
你必须像下面这样写
<form action="cal.php" method="post">
否则你的代码很好