【问题标题】:Php calculator answer wont show up in the browserPHP计算器答案不会显示在浏览器中
【发布时间】:2015-03-04 05:16:26
【问题描述】:

我尝试尽可能多地重新格式化我的代码,但我没有得到任何显示给浏览器的结果。如果你能仔细看看,也许让我知道我哪里出错了,那会很酷,谢谢。

<!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>

我觉得我的代码是正确的,但不确定为什么它没有在浏览器中显示答案。

提前致谢!

【问题讨论】:

    标签: php html forms http-post


    【解决方案1】:

    您在 HTML 中发布的方式不正确。

    <form action="cal.php" method="$_POST">
    

    将其更改为:

    <form action="cal.php" method="POST">
    

    【讨论】:

      【解决方案2】:
      method = "POST"
      

      而不是

      method = "$_POST"
      

      【讨论】:

        【解决方案3】:

        您只需要将表单的方法更改为“POST”

        你的方法是

        <form action="cal.php" method="$_POST">
        

        你必须像下面这样写

        <form action="cal.php" method="post">
        

        否则你的代码没问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-25
          • 2016-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-19
          相关资源
          最近更新 更多