Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php实例:计算表单数据-计算器的代码

php实例:计算表单数据-计算器的代码

发布时间:2016-12-08   编辑:www.jquerycn.cn
本文介绍一个php实例,用于计算根据表单提交的数据,有需要的朋友,可以作个参考。

1,计算表单中的数据,类似一个小型计算器。

<html>
 <head> 
  <title>计算表单数据-www.jbxue.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>

您可能感兴趣的文章:
php实例:计算表单数据-计算器的代码
PHP中计算时间差的几种方法
计算文件夹大小的php代码
计算php脚本执行时间的示例代码
php 时间计算问题学习总结
php计算距离的代码一例
计算机基础知识-计算机组成与原理
Php计算时间差 php日期转换的例子
php求1000以内质数(素数)
CSS Expression讲解

[关闭]