2012-06-10 45 views
2

當我在數字1和數字2中輸入數值,然後按下「添加」。它不顯示總增加值。請參閱下面的代碼。並建議我,問題是什麼,以及可以做些什麼。PHP粘性表格

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

回答

5
  1. 您呼應結果數據到<head>,所以它不會顯示出來。
  2. 你忘了回覆$add
  3. 您的<input> s類型爲button而不是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> 
+0

非常感謝DaveRandom的解決方案的工作很好..但同樣的問題,不顯示 – bleach64

+0

@MushfiqurRahman這可能是因爲我改變了val在我的服務器上測試表單的'action'的ue - 我剛剛編輯/修復,嘗試上面的版本。 – DaveRandom

+0

@MushfiqurRahman看到這個工作演示:http://codepad.viper-7.com/aao1vq – DaveRandom

0

如果你改變了這個

action="simple_calculator.php" 

通過

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