communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "forms" - download "simple calculator" - open demo of "simple calculator"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

forms - simple calculator was created/last modified on Sat 17 Apr 2004 11:48 AM

a simple php calculator

<?PHP

if(!$_POST['act'])
{
  echo
'
<form method="post" action="'
.$_SERVER['PHP_SELF'].'">
  <input type="text" name="z1" size="5">
  <select name="act">
   <option value="add">[ + ]</option>
   <option value="sub">[ - ]</option>
   <option value="div">[ / ]</option>
   <option value="mul">[ * ]</option>
   <option value="pro">[ % ]</option>
  </select>
  <input type="text" name="z2" size="5">
  <input type="submit" name="doit" value="calculate">
</form>
'
;
}else{
  if(
$_POST['act'] == 'add')
  {
   
$calc = ($_POST['z1']+$_POST['z2']);
   
$op = '+';
  }
  if(
$_POST['act'] == 'sub')
  {
   
$calc = ($_POST['z1']-$_POST['z2']);
   
$op = '-';
  }
  if(
$_POST['act'] == 'div')
  {
   
$calc = ($_POST['z1']/$_POST['z2']);
   
$op = '/';
  }
  if(
$_POST['act'] == 'mul')
  {
   
$calc = ($_POST['z1']*$_POST['z2']);
   
$op = '*';
  }
  if(
$_POST['act'] == 'pro')
  {
   
$calc = (($_POST['z2']*$_POST['z1'])/100);
   
$op = '% of';
  }
  echo
$_POST['z1'].' '.$op.' '.$_POST['z2'].' = '.$calc;
}

?>

learn more about some of the PHP functions used in this snippet: echo, else, for

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari