2

当我在数字 1 和数字 2 中输入数值,然后按“添加”。它不显示总附加值。请在下面查看我的编码。并建议我,问题是什么,可以做什么。

<html>
<head>
<title>Simple Calculator</title>
<?php
    if(isset($_POST['submitted'])){
        if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
        $add = ($_POST['number1'] + $_POST['number2']);
            echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
            }
        }



?>
<script type="text/javascript">


</script>
</head>

<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>

          <input type="button" name="add" value="Add" />
          <input type="button" name="minus" value="Minus" />
          <input type="button" name="multiply" value="Multiply" />
          <input type="button" name="divide" value="Divide" />
          <input type="reset" name="rest" value="Reset" />
          <input type="hidden" name="submitted" value="TRUE" />


</form>

</body>
</html>
4

2 回答 2

5
  1. 您将结果数据回显到 中<head>,因此不会显示。
  2. 你忘了回声$add
  3. <input>的 s 是 typebutton而不是submit,所以表单不会被提交到服务器。
  4. 因为您将先前输入的值回显到表单中,所以<input type="reset">可能不会做您想要/期望它做的事情。我认为最好将其作为另一个submit.
  5. 由于此表单仅影响下一页显示的内容,并且不会对服务器进行永久更改,因此您应该使用该GET方法而不是POST.

试试这个:

<html>
  <head>
    <title>Simple Calculator</title>
    <script type="text/javascript"></script>
  </head>
  <body>
    <h1>Simple Calculator</h1>
    <form action="simple_calculator.php" method="get">
      <p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
      <p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
      <input type="submit" name="add" value="Add" />
      <input type="submit" name="minus" value="Minus" />
      <input type="submit" name="multiply" value="Multiply" />
      <input type="submit" name="divide" value="Divide" />
      <input type="submit" name="reset" value="Reset" />
      <input type="hidden" name="submitted" value="1" />
    </form>
    <?php

        if (isset($_GET['submitted']) && !isset($_GET['reset'])) {

            echo "<div>";

            if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {

                if (isset($_GET['add'])) {

                    $result = $_GET['number1'] + $_GET['number2'];
                    echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;

                } else if (isset($_GET['minus'])) {

                    $result = $_GET['number1'] - $_GET['number2'];
                    echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;

                } else if (isset($_GET['multiply'])) {

                    $result = $_GET['number1'] * $_GET['number2'];
                    echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;

                } else if (isset($_GET['divide'])) {

                    $result = $_GET['number1'] / $_GET['number2'];
                    echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;

                }

            } else {

                echo "Invalid input";

            }

            echo "</div>";

        }

    ?>
  </body>
</html>
于 2012-06-10T13:05:17.753 回答
0

如果你改变这个,DaveRandom 的解决方案可以正常工作

action="simple_calculator.php"

经过

action="<?php echo $_SERVER['PHP_SELF'] ?>"
于 2012-10-01T14:05:48.930 回答