用PHP做了个简易的,不需要验证输入数据合法性的计算器,为啥点提交没响应呢?求大神帮助!
第一、输入页面代码:<html><head><title>计算器</title></head><fromaction="calwork.php"method="post"...
第一、输入页面代码:
<html>
<head>
<title>计算器</title>
</head>
<from action="calwork.php" method="post">
num1:<input size=10 type="text" name="num1"/><br>
nmu2:<input size=10 typ="text" name="num2"/><br>
运算符:<select name="oper">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br>
<input type="submit" name="submit" value="开始计算"/>
</from>
</html>
第二、计算逻辑代码
<?php
require_once "calclass.php";
if(isset($_REQUEST['num1'])){
$num1=$_REQUEST['num1'];
}
if(isset($_REQUEST['num2'])){
$num2=$_REQUEST['num2'];
}
if(isset($_REQUEST['oper'])){
$oper=$_REQUEST['oper'];
}
echo $num1;
?>
第三段代码,类:
<?php
class operres{
public function res($num1,$num2,$oper){
switch ($oper){
case "+":
return $num1+$num2;
break;
case "-":
return $num1-$num2;
break;
case "*":
return $num1*$num2;
break;
case "/":
return $num1/$num2;
break;
default;
echo "输入错误!";
}
}
}
$a=new operres;
echo $a->res($num1, $num2, $oper);
?> 展开
<html>
<head>
<title>计算器</title>
</head>
<from action="calwork.php" method="post">
num1:<input size=10 type="text" name="num1"/><br>
nmu2:<input size=10 typ="text" name="num2"/><br>
运算符:<select name="oper">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><br>
<input type="submit" name="submit" value="开始计算"/>
</from>
</html>
第二、计算逻辑代码
<?php
require_once "calclass.php";
if(isset($_REQUEST['num1'])){
$num1=$_REQUEST['num1'];
}
if(isset($_REQUEST['num2'])){
$num2=$_REQUEST['num2'];
}
if(isset($_REQUEST['oper'])){
$oper=$_REQUEST['oper'];
}
echo $num1;
?>
第三段代码,类:
<?php
class operres{
public function res($num1,$num2,$oper){
switch ($oper){
case "+":
return $num1+$num2;
break;
case "-":
return $num1-$num2;
break;
case "*":
return $num1*$num2;
break;
case "/":
return $num1/$num2;
break;
default;
echo "输入错误!";
}
}
}
$a=new operres;
echo $a->res($num1, $num2, $oper);
?> 展开
1个回答
展开全部
你是第一段旦含里面数据山余提交到第二段吗?
你的
require_once "calclass.php";包含时候
先执逗迟滚行
$a=new operres;
echo $a->res($num1, $num2, $oper);
这时候
$num1 2等变量都是空
你的
require_once "calclass.php";包含时候
先执逗迟滚行
$a=new operres;
echo $a->res($num1, $num2, $oper);
这时候
$num1 2等变量都是空
追问
那该怎么解决呢?
追答
if(isset($_REQUEST['num1'])){
$num1=$_REQUEST['num1'];
}
if(isset($_REQUEST['num2'])){
$num2=$_REQUEST['num2'];
}
if(isset($_REQUEST['oper'])){
$oper=$_REQUEST['oper'];
}
echo $num1;
class operres{
public function res($num1,$num2,$oper){
switch ($oper){
case "+":
return $num1+$num2;
break;
case "-":
return $num1-$num2;
break;
case "*":
return $num1*$num2;
break;
case "/":
return $num1/$num2;
break;
default;
echo "输入错误!";
}
}
}
$a=new operres;
echo $a->res($num1, $num2, $oper);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询