2015-11-18 45 views
1

嘿所以我正在做一個快速的PHP程序,允許簡單的數學計算。而不是讓輸出到_POST的不同頁面,我希望他們都去同一頁面。 所以我嘗試使用一個函數,它會看到_POST數據並區分輸入並根據輸入運行不同的函數。

任何想法即時做錯了什麼?

<h1>Add:</h1> 
    <form action="result.php" method="post"> 
    #1: <input type="text" name="a1"><br> 
    #2: <input type="text" name="a2"><br> 
    <input type="hidden" name="action" value="add"> 
    <input type="submit" name="addsubmit"> 
    </form> 
<hr /> 
<h1>Subtract:</h1> 
    <form action="result.php" method="post"> 
    #1: <input type="text" name="s1"><br> 
    #2: <input type="text" name="s2"><br> 
    <input type="hidden" name="action" value="subtract"> 
    <input type="submit" name="subsubmit"> 
    </form> 
<hr /> 
<h1>Multiply:</h1> 
    <form action="result.php" method="post"> 
    #1: <input type="text" name="m1"><br> 
    #2: <input type="text" name="m2"><br> 
    <input type="hidden" name="action" value="multiply"> 
    <input type="submit" name="multisubmit"> 
    </form> 
<hr /> 
<h1>Divide:</h1> 
    <form action="result.php" method="post"> 
    #1: <input type="text" name="d1"><br> 
    #2: <input type="text" name="d2"><br> 
    <input type="hidden" name="action" value="divide"> 
    <input type="submit" name="divsubmit"> 
    </form> 



</body> 

~~~

<?php 



$a1 = $_POST["a1"]; 
$a2 = $_POST["a2"]; 
$s1 = $_POST["s1"]; 
$s2 = $_POST["s2"]; 
$m1 = $_POST["m1"]; 
$m2 = $_POST["m2"]; 
$d1 = $_POST["d1"]; 
$d2 = $_POST["d2"]; 


function add($num1, $num2){ 
    echo $num1 + $num2; 
} 

function subtract($num1, $num2){ 
    echo $num1 - $num2; 
} 

function multiply($num1, $num2){ 
    echo $num1 * $num2; 
} 

function divide($num1, $num2){ 
    echo $num1/$num2; 
} 

function operate(){ 


    switch($_POST['submit']) { 
     case 'addsubmit': 
      add($a1, $a2); 
      break; 
     case 'subsubmit': 
      subtract($s1, $s2); 
      break; 
     case 'multisubmit': 
      multiply($m1, $m2); 
      break; 
     case 'divsubmit': 
      divide($d1, $d2); 
      break; 
    } 

} 



?> 

<div id="1"> 
     <h1>Addition:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="2"> 
     <h1>Subtraction:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="3"> 
     <h1>Multiplication:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="4"> 
     <h1>Division:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
    </div> 

    <button> <a href="/workspaces/testgrounds/addition.php">Back to main page</a></button> 
</body> 

~~ EDIT1:

First Errors || Second Errors -

+0

什麼不工作?你會得到什麼錯誤? –

+0

1.你需要在某處調用'operate()'函數 - 目前加載文件不會運行任何代碼,只需定義一堆函數即可。 2.由於每次只設置2個POST變量,所以前8行代碼會產生至少6個未定義的索引錯誤 - 您應該使用isset來檢查,並且您還可以將變量初始化放在切換大小寫區塊 – Steve

+0

您正在切換$ _POST ['submit'],該文件未發佈(沒有名稱爲「submit」的表單元素)。 – WillardSolutions

回答

0
<?php 

$a1 = $_POST["a1"]; 
$a2 = $_POST["a2"]; 
$s1 = $_POST["s1"]; 
$s2 = $_POST["s2"]; 
$m1 = $_POST["m1"]; 
$m2 = $_POST["m2"]; 
$d1 = $_POST["d1"]; 
$d2 = $_POST["d2"]; 


function add($num1, $num2){ 
    return $num1 + $num2; 
} 

function subtract($num1, $num2){ 
    return $num1 - $num2; 
} 

function multiply($num1, $num2){ 
    return $num1 * $num2; 
} 

function divide($num1, $num2){ 
    return $num1/$num2; 
} 

function operate() { 

    switch($_POST['action']) { 
     case 'add': 
      return add($a1, $a2); 
      break; 
     case 'subtract': 
      return subtract($s1, $s2); 
      break; 
     case 'multiply': 
      return multiply($m1, $m2); 
      break; 
     case 'divide': 
      return divide($d1, $d2); 
      break; 
    } 
} 

?> 

    <div id="1"> 
     <h1>Addition:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="2"> 
     <h1>Subtraction:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="3"> 
     <h1>Multiplication:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
     <hr /> 
    </div> 

    <div id="4"> 
     <h1>Division:</h1> 
     Your answer is <?php echo operate(); ?><br /> 
    </div> 

    <button> <a href="/workspaces/testgrounds/addition.php">Back to main page</a></button> 
</body> 

你應該在今年年初增加一些isset()函數測試你的變量賦值,太。

+0

行動有幫助,但我仍然有錯誤,所以我移動了變量initializa在開關的情況下,並擺脫了所有的錯誤,但現在它不會計算任何東西。 奇怪。 –

+0

如果它位於operate()函數內部,則必須調用該函數使其計算。你看到什麼錯誤? – WillardSolutions

+0

http://prntscr.com/94cd2o 沒有更多的錯誤,只是非計算在HTML –