Single form multiple button calculator
<code>
/// ///authour: thushara kasun ranawaka ///program:- simple calculator
///
<html>
<body>
<form action="hello.php" method="post">number 1:<input type="text"
name="no1"></br>
number 2:<input type="text" name="no2"></br>
<button type="submit" name="add">+</button>
<button type="submit" name="sub">-</button>
<button type="submit" name="mul">*</button>
<button type="submit" name="div">/</button>
<button type="submit" name="pow">^</button>
<?php
if(isset($_POST['add'])) { echo '</br>'.'answer is:- '.add($_POST['no1'],$_POST['no2']); }
else if(isset($_POST['sub'])){echo '</br>'.'answer is:- '.sub($_POST['no1'],$_POST['no2']); }
else if(isset($_POST['mul'])) { echo '</br>'.'answer is:- '.mul($_POST['no1'],$_POST['no2']); }
else if(isset($_POST['div'])){echo '</br>'.'answer is:- '.div($_POST['no1'],$_POST['no2']); }
else if(isset($_POST['pow'])) { echo '</br>'.'answer is:- '.pow1($_POST['no1'],$_POST['no2']); }
function add($x,$y){
if(filter_var($x, FILTER_VALIDATE_INT) && filter_var($y, FILTER_VALIDATE_INT))return $x+$y;
else return "please enter valid numaric numbers";
}
function sub($x,$y){
if(filter_var($x, FILTER_VALIDATE_INT) && filter_var($y, FILTER_VALIDATE_INT))return $x-$y;
else return "please enter valid numaric numbers";
}
function mul($x,$y){
if(filter_var($x, FILTER_VALIDATE_INT) && filter_var($y, FILTER_VALIDATE_INT))return $x*$y;
else return "please enter valid numaric numbers";
}
function div($x,$y){
if(filter_var($x, FILTER_VALIDATE_INT) && filter_var($y, FILTER_VALIDATE_INT))return $x/$y;
else return "please enter valid numaric numbers";
}
function pow1($x,$y){
$ans=1;
if(filter_var($x, FILTER_VALIDATE_INT) && filter_var($y, FILTER_VALIDATE_INT)){
for($i=0;$i<$y;$i++){
$ans=$ans*$x;
}
return $ans;
}
else return "please enter valid numaric numbers";
}
?>
</body>
</html>
thank you everybody see you soon :)
Comments
Post a Comment