这就是我输入的内容。对于这种表单方法,我想在答案输入字段中得到答案。 PHP无法正常工作。我无法理解什么是错的
<form action="index.html" method="get">
<h1>Calculator</h1>
<a>Num 1</a> <input type="text" name="num1" value=""><br><br>
<a>Num 2</a> <input type="text" name="num2" value=""><br><br>
<a>Answer</a> <input type="text" name="ans" value=""><br><br>
<button type="button" name="add">+</button>
<button type="button" name="subtract">-</button>
<button type="button" name="multiply">*</button>
<button type="button" name="devide">/</button>
</form>
<?php
if (isset($_GET["add"]) == 'add') {
$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$answer = $_GET['ans'];
$_GET['ans'] = $_GET['num1'] + $_GET['num2'];
$_GET['ans'];
}
&GT;
答案 0 :(得分:1)
首先错误的是这一行:
if (isset($_GET["add"]) == 'add') {
isset()
会返回true或false,因此虽然您可以将其与add
进行比较,但总体上会导致true
,但它没有任何意义。
通过为所有按钮指定相同的名称和不同的值,您可以更轻松地实现这一目标:
<button type="submit" name="action" value="add">+</button>
<button type="submit" name="action" value="subtract">-</button>
<button type="submit" name="action" value="multiply">*</button>
<button type="submit" name="action" value="divide">/</button>
请注意,我还将type
更改为submit
,button
类型不会提交您的表单!
另外,将action="index.html"
更改为action="index.php"
,.html
文件根本不会执行任何PHP代码。
现在您可以通过一个简单的开关选择您的操作:
switch ($_GET['action']) {
case 'add':
$answer = $_GET['num1'] + $_GET['num2'];
break;
case 'subtract':
// your substract code
break;
case 'multiply':
// your multiply code
break;
case 'divide':
// your divide code
break;
}
最后,但并非最不重要的是,在HTML已经打印完毕后计算出来。您不能只回写$_GET
并期望已经打印的HTML字段发生变化。因此,将PHP放在前面并将结果输出到输入字段:
<?php
if ( isset($_GET['action']) ) {
switch ($_GET['action']) {
case 'add':
$answer = $_GET['num1'] + $_GET['num2'];
break;
case 'subtract':
// your substract code
break;
case 'multiply':
// your multiply code
break;
case 'divide':
// your divide code
break;
}
}
?>
<form action="index.php" method="get">
Num 1 <input type="text" name="num1"><br><br>
Num 2 <input type="text" name="num2"><br><br>
Answer <input type="text" name="ans" value="<?php echo $answer; ?>"><br><br>
<button type="submit" name="action" value="add">+</button>
<button type="submit" name="action" value="subtract">-</button>
<button type="submit" name="action" value="multiply">*</button>
<button type="submit" name="action" value="divide">/</button>
</form>
答案 1 :(得分:1)
你应该按下你的按钮=&#34;提交&#34;
答案 2 :(得分:0)
检查一下,我没有测试过。
解决方案1
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="#">
Enter the First Number <input type="text" name="number1"> <br><br>
Enter the Second Number <input type="text" name="number2"><br><br>
Choose the operation
Addition <input type="radio" name="op" value="add" checked>
Subtraction <input type="radio" name="op" value="sub">
Multiplication <input type="radio" name="op" value="mul">
Division<input type="radio" name="op" value="div"><br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
if ( $op == "add" ) {
echo "$number1 + $number2 = ";
echo $number1 + $number2;
}
if ( $op == "sub" ) {
echo "$number1 - $number2 = ";
echo $number1 - $number2;
}
if ( $op == "mul" ) {
echo "$number1 * $number2 = ";
echo $number1 * $number2;
}
if ( $op == "div" ) {
echo "$number1 / $number2 = ";
echo $number1 / $number2;
}
?>
解决方案2
<html>
<head>
<title>Simple PHP Calculator</title>
</head>
<body>
<form method="POST" action="#">
<input type="text" name="value1">
<input type="text" name="value2">
<select name="action">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="submit" name="submit" value="Calculate Now'">
</form>
</body>
</html>
<?php
if(isset($_POST["submit"]))
{
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$action = $_POST["action"];
if($action==”+”)
{
echo "<b>Addition : - </b>" . ($value1+$value2) . "\n\n";
}
if($action==”-“)
{
echo "<b>Subtraction : -</b>" . ($value1-$value2). "\n\n";
}
if($action==”*”)
{
echo "<b>Multiplication : -</b>" . ($value1*$value2). "\n\n";
}
if($action==”/”)
{
echo "<b>Division : - </b>" . ($value1)/($value2) . "\n\n";
}
}
?>