PHP粘贴形式

时间:2012-06-10 13:00:42

标签: php html

当我在数字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>

2 个答案:

答案 0 :(得分:5)

  1. 您正在将结果数据回显到<head>,因此不会显示。
  2. 您忘了回复$add
  3. 您的<input>类型为button而非submit,因此该表单不会提交给服务器。
  4. 因为您将之前输入的值回显到表单中,<input type="reset">可能无法执行您希望/期望的操作。我认为最好将其作为另一个submit实现。
  5. 由于此表单仅影响下一页显示的内容,并且不会对服务器进行永久性更改,因此您应使用GET方法,而不是POST
  6. 试试这个:

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

答案 1 :(得分:0)

如果你改变这个

,DaveRandom的解决方案可以正常工作
action="simple_calculator.php"

通过

action="<?php echo $_SERVER['PHP_SELF'] ?>"