2015-11-18 46 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 –