Monday, September 26, 2016

Calculator in PHP using Drop Down Menu

A simple calculator program that I wrote in PHP using Drop Down Menu to select the mathematical operator such as addition, subtraction, multiplication and division using combo box in HTML.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 

My mobile number here in the Philippines is 09173084360.







Sample Program Output


Program Listing

<html>
<head>
<?php
error_reporting(0);

 $a =  $_REQUEST['num1'];
 $b =  $_REQUEST['num2'];
?>

<title>Simple Calculator</title>
</head>
<style type="text/css">
 body
 {
 background-color:yellow;
 font-family:arial;
 font-size:14px;
 font-weight:bold;
 }

 #result
 {
 color:green;
 font-family:arial;
 font-size:16px;
 }



 input[type="text"] {
 height: 50px;
 width: 50px;
 font-family:arial;
 font-size:25px;
 font-weight:bold;
}

#opt{
 width:250px;   
}
</style>
<script language="javascript">
 function validate_form()
 {
 var num1 = document.entryform.num1;
 var num2 = document.entryform.num2;
 if (num1.value=="" || num1.value==null)
 {
 alert("Please specify value on first box");
 num1.focus();
 return false;
 }
 else if (num2.value=="" || num2.value==null)
 {
 alert("Please specify value on second box");
 num2.focus();
 return false;
 }
 }
</script>
<body>
<?php
if (isset($_REQUEST['clear']))
 {
  $a="";
  $b="";  
 }
 ?>
<form method="post" onSubmit="return validate_form(this)" name="entryform">
<fieldset>
 <legend>Simple Calculator</legend>
 <input type="text" name="num1" value="<?php echo $b; ?>"  autofocus>
 <select name="opt_math">
 <option value="+">+
 <option value="-">-
 <option value="*">*
 <option value="/">/
 </select>
 <input type="text" name="num2" value="<?php echo $a; ?>"><br><br>
 <input type="submit" name="submit" value="Compute"> &nbsp; &nbsp;
<input type="submit" name="clear" value="Clear">
 <br><br>

 <span id="result">
 Result is
 <strong>
 <?php
if ($_REQUEST['opt_math']=="+"){
 echo  $a+$b;
 }
 else if ($_REQUEST['opt_math']=="-"){
 echo $a-$b;
 }
 else if ($_REQUEST['opt_math']=="/"){
 echo $a/$b;
 }
 else if ($_REQUEST['opt_math']=="*"){
 echo $a*$b;
 }

 ?>
 </strong>
 </span><br><br><br>
  </fieldset>
 </form>
 </body>
 </html>



No comments:

Post a Comment