嘿所以我正在制作一个快速的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:
答案 0 :(得分: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()测试。