Thursday, September 5, 2013

Calculator in php

PHP assingment

Create Simple arithmetic calculator with basic validation.


First create a calc.php file  and then add the following code to it 


<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Calculator </title>
</head>
<body bgcolor="lightgray">
<?php error_reporting(E_ALL^E_NOTICE)?>
<div align='Center'>
<h1>Calculate the Values</h1>
<form name="form1" action="calc.php" method="POST" >
<table >
<tr><td>Enter First Value</td><td><input type="text" name="value1" /></td></tr>
<tr><td>Enter Second Value</td><td><input type="text" name="value2" /></td></tr>
<tr><td>Operation You want to Perform </td><td><select name="operation">  
<option value="Add">Add</option>
<option value="Minus">Minus </option>
<option value="Multiply">Multiply </option>
<option value="Divide">Divide</option>
</select></td>
</tr>
<tr><td colspan="2" align="Center"><input type="Submit" name="submit1" value="Calculate" /></td></tr>
<tr><font color="red"><td>Total is :</td><td><?php
//echo $Value1." Total " .$Value2;
if(isset($_POST['submit1'])){ // check the form is submited or not
$Value1=$_POST['value1'];
$Value2=$_POST['value2'];
$operation=$_POST['operation'];

switch($operation){
case "Add": echo $Value1 + $Value2;
break;
case "Minus": echo $Value1 - $Value2;
break;
case "Multiply": echo $Value1 *$Value2;
break;
case "Divide": echo $Value1 / $Value2;
break;
}

//echo $Value1." Total " .$Value2 .$operation;
}
?></td></font></tr>
</table

</form>
</div> 
</body>

</html>

1 comment:

  1. i really liked all the programs, they were really very helpfull 2 me, thnks gopal for helping, and keep posting if u have anymore like this, thnks once agn

    ReplyDelete