Calculate add,sub,mul using php

Steps:

1. Create new html file and copy calculate.html code and save it as calculate.html.

2. Create new php file and copy calculation.php code and save it as calculation.php.

3. Run calculate.html, then it will display the html data.

4. Enter value1, value2 and press add or sub or mul button and press calculate button.

5. Then it will transfer to calculation.php which displays the output.

calculate.htm:

[php]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>calculation</title>
</head>

<body>
<form method=”post” action=”calculation.php”>
Value 1: <input type=”text” name=”val1″ size=”7″><br /><br />
Value 2: <input type=”text” name=”val2″ size=”7″>
<p>
<input type=”radio” name=”calculate” value=”add”  />
add
<input type=”radio” name=”calculate” value=”sub”  />
sub
<input type=”radio” name=”calculate”  value=”mul”/>
mul
</p>
<input type=”submit” name=”submit” value=”calculate”/>
</form>
</body>
</html>

[/php]

It displays the output as:
Value 1: Value 2:
add
sub
mul

calculation.php:
[php]
</form><form action=”calculation.php” method=”post”><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head><body>
<?php
if(($_POST[‘val1’]==””) || ($_POST[‘val2’]==””))
{
header(“location: calculate.html”);
exit();
}
if($_POST[‘calculate’]==”add”)
{
$result=$_POST[‘val1’]+$_POST[‘val2’];
}
else if($_POST[‘calculate’]==”sub”)
{
$result=$_POST[‘val1’]-$_POST[‘val2’];
}
else if($_POST[‘calculate’]==”mul”)
{
$result=$_POST[‘val1’]*$_POST[‘val2’];
}
echo “Calculation is: $result”;
?>
</body>
</html></form><form action=”calculation.php” method=”post”>
[/php]
An example output for add by giving the input as value1 as 5 and value2 as 5 is:
Calculation is: 10

No comments yet.

Leave a comment

Request a Free SEO Quote