php表单请求获得数据求和。。。

获得表单请求的值:

由一个页面跳转到另一个页面获得结果的案例:

request.php

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8;" />

<title>计算请求</title>

</head>

<body>

<form action="result.php" method="post">

<input type="text" name="grade"/>

<input type="submit" value="开始计算"/>

</form>

</body>

</html>

result.php

<?php

$grade=$_REQUEST['grade'];//grade-->和表单中的name值一样

$arr=explode(" ",$grade);//以空格拆分字符串,并得到数组结果

print_r($arr);

$res=0;

for($i=0;$i<count($arr);$i++){

$res+=$arr[$i];

}

echo "<br/>ALL=".$res;

echo "<br/>AVG=".(round($res/count($arr),0));//round(12.334,2)//四舍五入的方法

?>

在当前页面获得结果的案例:

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8;" />

<title>计算请求</title>

</head>

<?php

error_reporting(E_ALL ^ E_NOTICE);//让notice级别的错误不提示的方法,还有一种方式就是在php.ini文件中加一句配置:error_reporting = E_ALL & ~E_NOTICE;

$grade=$_REQUEST['grade'];//grade-->和表单中的name值一样

$arr=explode(" ",$grade);//以空格拆分字符串,并得到数组结果

print_r($arr);

$res=0;

for($i=0;$i<count($arr);$i++){

$res+=$arr[$i];

}

?>

<body>

<form action="jisuanRequest.php" method="post">

<input type="text" name="grade" value="<?php echo $grade;?>"/>// value="<?php echo $grade;?>"记录发送请求之后输入的数字

<input type="submit" value="开始计算"/>

</form>

<?php //php中可以从一个<?php ?>在另外一个<?php ?>中直接取值

echo "<br/>ALL=".$res;//上面的<?php ?>计算好的值在这里输出

echo "<br/>AVG=".(round($res/count($arr),0));//round(12.334,2)//四舍五入的方法

?>

</body>

</html>