1,计算表单中的数据,类似一个小型计算器。
<html> <head> <title>计算表单数据-www.jb200.com</title> </head> <body> <form action = "calc.php" method = "post"> 值 1: <input type = "text" name = "val1" size = "10"> 值 2: <input type = "text" name = "val2" size = "10"> <br> 计算: <br> <input type = "radio" name = "calc" value = "add"> 加 <input type = "radio" name = "calc" value = "sub"> 减 <input type = "radio" name = "calc" value = "mul"> 乘 <input type = "radio" name = "calc" value = "div"> 除 <hr> <input type = "submit" value = "计算"> <input type = "reset" value = "清除"> </form> </body> </html>
2,php程序文件: calc.php
<html>
<head>
<title>计算结果</title>
</head>
<body>
<?php
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
$calc = $_POST['calc'];
if( is_numeric( $val1 ) && is_numeric( $val2 ) )
{
if( $calc != null )
{
switch( $calc )
{
case "add" : $result = $val1 + $val2; break;
case "sub" : $result = $val1 - $val2; break;
case "mul" : $result = $val1 * $val2; break;
case "div" : $result = $val1 / $val2; break;
}
echo( "结果为: $result" );
}
}
else
{
echo( "数据无效,请再试一次。" );
}
?>
</body>
</html>