这是我的表格
<form>
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['$submit'])){
$result1 = $_GET['$num1'];
$result2 = $_GET['$num2'];
$operator = $_GET['$operator'];
switch ('$operator'){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
当我单击“计算”按钮时,没有看到答案,我尝试修复所有错误,但在我看来,似乎是开关方面的问题。
答案 0 :(得分:0)
尝试一下:-
获取值和切换参数时您犯了一些错误。
就像您正在使用值$_GET['$num1']
一样,您需要使用$_GET['num1']
因为您只需要传递变量开关($ operator)而不是引号switch ('$operator')
。
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case 'Add':
$FResult =$result1 + $result2;
echo "Result:"+ $FResult ;
break;
case 'Subtract':
$FResult =$result1 - $result2;
echo "Result:"+ $FResult ;
break;
case 'Multiply':
$FResult =$result1 * $result2;
echo "Result:"+ $FResult ;
break;
case 'Divide':
$FResult =$result1 / $result2;
echo "Result:"+ $FResult ;
break;
default :
echo "wrong";
}
}
?>
答案 1 :(得分:0)
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
Another way:
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
答案 2 :(得分:0)
您犯的错误:
1)。您将变量直接传递到get元素,即应该像$ _GET ['submit']而不是$ _GET ['$ submit'];
2)。您传递的是字符串而不是变量,即switch('$ operator')应该是switch($ operator)
检查出您犯错的地方
<form method="GET" action="">
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
答案 3 :(得分:0)
您正在寻找PHP解决方案,这是一个:-
<?php
if (isset($_POST['submit'])){
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch($operator){
case "Add":
$res = +$result1 + +$result2;
break;
case "Subtract":
$res = +$result1 - +$result2;
break;
case "Multiply":
$res = +$result1 * +$result2;
break;
case "Divide":
$res = +$result1 / +$result2;
break;
}
}
?>
<form method="post" action="">
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<input type="submit" name="submit" id="submit" value="Calculate" />
<p>Your answer is : <input name="res" value="<?php echo $res; ?>"></p>
</form>
这就是您想要的。
答案 4 :(得分:0)
您应该在选择标签(计算器要使用的操作符)<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Name</label>
<div class="col-xs-10">
<input type="text" class="form-control" id="name" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-xs-2 control-label">Email</label>
<div class="col-xs-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="sex" class="col-xs-2 control-label">Sex</label>
<div class="col-xs-10">
<select name="sex" class="auto-width setup-inline form-control input-medium" required>
<option value="">---Please select---</option>
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10 text-right">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
中设置选项值。
更改您将<option value="">
输入type="text"
type="number"
答案 5 :(得分:-1)
您要做的是,您正在后端进行计算,并且表单正在刷新页面,但未在页面上的任何位置显示结果。最好与JQuery一起使用,以便无需刷新页面即可立即获得结果。只需检查以下代码:-
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#submit").on('click', function(){
var num1 = $("#num1").val() ;
var num2 = $("#num2").val() ;
var operator = $("#operator").val() ;
var result = 0 ;
switch(operator){
case "Add":
result = +num1 + +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Subtract":
result = +num1 - +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Multiply":
result = +num1 * +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Divide":
result = +num1 / +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
}
});
});
</script>
<div>
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<button name="submit" id="submit">Check for the answer</button>
<p>Your answer is : <span id="res"></span></p>
</div>
这成功运行。