Showing posts with label Calculator in PHP. Show all posts
Showing posts with label Calculator in PHP. Show all posts

Monday, July 1, 2019

Math Calculator in PHP

Here is a simple math calculator that can perform basic arithmetic operations like addition, subtraction, multiplication, and division using CSS, HTML, and PHP.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com


Sample Program Output

Program Listing

<!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>Calculator in PHP</title>
<style type="text/css">
<!-- 
.btn {color: #0000CC; border: #FF0000 1px solid; width:150px; height:35px; font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:20px;}
.button {background-color: #999999; border:#999900 1px solid; width:100px; height:30px; color:#FFF; font-style:bold;}
.h1 {font-family:Verdana, Arial, Helvetica, sans-serif ; font-size:24px; color:#FFFFFF;}
.label {font-family:Verdana, Arial, Helvetica, sans-serif; font-size:20px;}
.style3 {color: #FF0000; font-weight: bold; font-size:25px;  }
.style4 {color: #FF0000; font-style: italic ; font-size:12px ; padding-left:20px;}
.tr {background-color: #999999;}
.tr1 {background-color: #CC9900; border: #CCCCCC 1px solid; height:30px;}
.style12 {background-color: #999999; color: #CCFF00; }
.style14 {font-size: 24px; color: #CCFF00; }
-->
body {
 font-family:arial;
}

</style>

<script type="text/JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>
</head>

<body >
<p>&nbsp;</p>
<form action="" method="post" name="form1" id="form1" >
  <?php
  error_reporting(0);
 //start of the code of PHP Main
if (isset($_POST['Submit']))                 // set the submit button when click
{

$value1=$_POST['value1'];                 // setting variables of the field 1 and field 2
$value2=$_POST['value2'];
$operation=$_POST['operatorbutton'];  //setting up variables with radio button
$Fresult='';
if ($value1=='' ) {                               //check the field if it is empty and post it with javascript msgbox
echo '<script language="javascript" >'  ; 
echo 'alert("Field 1 is required");';
echo'   </script>';
   }
if  ($value2=='') {                               //check the field if it is empty and post it with javascript msgbox
echo '<script language="javascript" >'  ;
echo 'alert("Field 2 is required");';
echo'   </script>';
   }
//start of the operation
if  ($operation=='') {
echo '<script language="javascript" >'  ;
echo 'alert("Division of zero is not allowed");';
echo'   </script>';}   //checking of operator if it is null
elseif($operation == 'add')   {
//addition
    $Fresult=$value1+$value2;}
    elseif ($operation == 'sub') {
//subtraction
    $Fresult=$value1-$value2;}
elseif ($operation == 'mul') {
//multiplication
$Fresult=$value1*$value2;}
elseif ($operation == 'div') 
//division
// checking if if is divided in zero and post it in a javascript
if ($value2 =='0') 
{     
echo '<script language="javascript" >'  ;
echo 'alert("Division of zero is not allowed");';
echo'   </script>';
          
  }
else   //else perform division
{$Fresult=$value1/$value2;}
}
        
     //setting all radio button to unchecked
$add = 'unchecked';
$subtract = 'unchecked';
$multiply = 'unchecked';
$divide = 'unchecked';
//setting variable for selected operator button
$selected_button = $_POST['operatorbutton'];
if ($selected_button == 'add') 
{
$add = 'checked';
}
else if ($selected_button == 'sub') 
{
$subtract = 'checked';
}
else if ($selected_button == 'mul') 
{
$multiply = 'checked';
}
else if ($selected_button == 'div') 
{
$divide = 'checked';
}
} //end of main

?>
  <table width="764" border="0" align="center" cellpadding="4" style="border:#CC9900 1px solid;">
    <tr>
      <th colspan="3" bgcolor="#CC6600" scope="col" class="tr1"><h1 class="h1">Calculator in PHP</h1></th>
    </tr >
    <tr>
      <td width="26%"><div align="right" class="label">First Number </div></td>
      <td width="1%"><span class="style3">:</span></td>
      <td width="73%" ><input name="value1" type="text" class="btn" id="value1" value="<?php echo trim($_POST['value1']); ?>" onkeypress=" return isNumberKey(event)"   /></td>  
      <!--display the error-->
    </tr>
    <tr>
      <td height="30"><div align="right" class="label">Second Number </div></td>
      <td><span class="style3">:</span></td>
      <td><input name="value2" type="text" class="btn" id="value2" value="<?php echo trim( $_POST['value2']); ?>" onkeypress=" return isNumberKey(event)"/></td>
    </tr>
    <tr class="tr">
      <td height="24"><div align="right" class="label">Math Operator</div></td>
      <td class="style3">:</td>
      <td  >
        <span class="style12">
        <input name="operatorbutton" type="radio" checked="checked" <?PHP print $add; ?>; value="add" />
        
        Addition
        <input name="operatorbutton" type="radio" value="sub" <?PHP print $subtract; ?> />
        Subtraction
        <input name="operatorbutton" type="radio" value="mul" <?PHP print $multiply; ?>/>
        Multiplication
        <input name="operatorbutton" type="radio" value="div" <?PHP print $divide; ?> />
        Division</span><span class="style14">&nbsp;</span></td>
    </tr>
    <tr>
      <td height="26"><div align="right" class="label">Result</div></td>
      <td class="style3">:</td>
      <td class="style3"><?php echo  $Fresult; ?></td>
    </tr>
    <tr>
      <td class="style4"><div align="center"></div>
          <div align="center"></div></td>
      <td>&nbsp;</td>
      <td><input name="Submit" type="submit" class="button"  value="Calculate"/>
        <input name="cmdReset" type="submit" class="button" id="cmdReset" onclick="MM_callJS('cleartext()')" value="Clear" />
        <label></label></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center" class="tr1"></div></td>
    </tr>
  </table>
</form>
</body>
</html>

<!--  JAVASCRIPT FOR NOT ALLOWING THE USER TO ENTER A CHARACTER TEXT-->
 <script language="javascript" >
 function isNumberKey(evt)
  {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 45 || charCode > 57))
return false;
return true;
  } </script>
  
<script language="javascript" >
 function cleartext()
  {
var form = document.forms[0];
//purpose?: to make the form textfield in to null
form.value1.value = "";
form.value2.value = "";
  } </script>


Thursday, February 26, 2015

Calculator Using Radio Buttons in PHP

One of the most enjoying past time that I would like to spend is to write a program it becomes one of my favorite hobbies during my college days. In this article I would like to share with you one of the most common programming problems in PHP how to write a simple calculator using radio buttons in PHP. Some of us think this kind of problem is very simple yes they are for some people who has already learned some experience in PHP programming but for some that are new in PHP problem it can gives us some headache if you don't how to figure it out. Our program will ask the user to enter two number in our text field and then our program will provide four radio buttons addition, subtraction, multiplication and division. Once the user select for example addition and click the solve button it will display the sum of the two numbers. I also added a clear button that enables the user to clear the text field and allows them to enter new values. 

The challenge that I face in writing this program is that I want to keep or retain the value of my selection radio button for example the addition radio button I want to retain its value after i click the solve button. In this program you will know how to accomplish such task with each. I have a great time writing this program I hope you will find my work useful in your quest in learning PHP programming.

If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360



Sample Output of Our Program

Program Listing

<head>
<?php
error_reporting(0);

 $first = $_REQUEST['first'];
 $second= $_REQUEST['second'];

 $first2 = (int)$first;
 $second2 = (int)$second;


if (isset($_REQUEST['solve'])) {
      
 $op = $_REQUEST['op'];
    
if ($op == 'add') {
 
   $add = $first2 + $second2;
 
 $title .="The sum of ".$first2. " and " .$second2. 
          " is " .$add.".";
     }  
if ($op == 'subtract') {
 
   $subtract = $first2 - $second2;
 
 $title .="The difference between ".$first2. " and " .$second2. 
          " is " .$subtract.".";
$op = ' checked="checked"';
     }    

if ($op == 'times') {
 
   $product = $first2 * $second2;
 
 $title .="The product of ".$first2. " and " .$second2. 
          " is " .$product.".";
     }    
if ($op == 'divide') {
 
   $quotient = $first2 / $second2;
 
 $title .="The quotient of ".$first2. " and " .$second2. 
          " is " .$quotient.".";
     }  
}  
 
    if(isset($_REQUEST['clear'])) {
      $first2 = " ";
      $second2 = " ";
      $result= " ";
   }
?>

<title>Calculator in PHP</title>
</head>
<style>
body {
     background-color: lightgreen;
font-family:arial;
font-style:bold;
font-size:22px; 
     font-family:arial;
     color:blue;
}
  
input
{
  -moz-border-radius: 15px;
    border-radius: 15px;
    border:solid 3px BLUE;
    padding:8px;
}
input {
  display: table-cell;
}

 label{
    display: table-cell;
    text-align: justify;
}
div.row{
    display:table-row;
}

/***FIRST STYLE THE BUTTON***/
button#gobutton{
cursor:pointer; /*forces the cursor to change to a hand when the button is hovered*/
padding:5px 25px; /*add some padding to the inside of the button*/
background:white; /*the colour of the button*/
border:1px solid #33842a; /*required or the default border for the browser will appear*/
/*give the button curved corners, alter the size as required*/
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
/*give the button a drop shadow*/
-webkit-box-shadow: 0 0 4px rgba(0,0,0, .75);
-moz-box-shadow: 0 0 4px rgba(0,0,0, .75);
box-shadow: 0 0 4px rgba(0,0,0, .75);
/*style the text*/
color:blue;
font-size:1.1em;
}
/***NOW STYLE THE BUTTON'S HOVER AND FOCUS STATES***/
button#gobutton:hover, button#gobutton:focus{
background-color :brown; /*make the background a little darker*/
/*reduce the drop shadow size to give a pushed button effect*/
-webkit-box-shadow: 0 0 1px rgba(0,0,0, .75);
-moz-box-shadow: 0 0 1px rgba(0,0,0, .75);
box-shadow: 0 0 1px rgba(0,0,0, .75);
}

 </style>

<body>

<div style='width:800px;margin:auto'>
<form>
<H2> Calculator in PHP </h2>
<div class="row"><label> FIRST VALUE 
<input type="text" id="first" name="first" placeholder="enter first value"
 value="<?php echo $first; ?>" autofocus required size=10></label></div><br>
<div class="row"> <label> SECOND VALUE
<input type="text" id="second" name="second" placeholder="enter second value"
 value="<?php echo $second; ?>" autofocus required size=10></label> </div>
<br>
<input type="radio" name="op"  value="add" <?php if($_REQUEST['op'] == "add") echo "checked"; ?>>ADDITION
<input type="radio" name="op"  value="subtract"  <?php if($_REQUEST['op'] == "subtract") echo "checked"; ?> >SUBTRACTION
<input type="radio" name="op"  value="times"  <?php if($_REQUEST['op'] == "times") echo "checked"; ?>>MULTIPLICATION
<input type="radio" name="op"  value="divide" <?php if($_REQUEST['op'] == "divide") echo "checked"; ?>>DIVISION
<br><br>
<button id="gobutton" type="submit" name="solve" id="solve" value="answer"
 title="Click here to find the result.">Solve</button>
<button id="gobutton" type="submit" name="clear" id="clear" value="answer"
title="Click here to clear text fields.">Clear</button>
</form>
<br> 
<?php
echo $title;
?>
</body> </div>
</html>



Friday, May 30, 2014

Simple Calculator in PHP

This simple program in PHP will allow the user to perform basic mathematical operations using PHP as your web scripting programming language it a simple calculator program in PHP. Before we can accept values from the use first we make a web form layout that will accept input values from our user. Their are two text field in HTML the first text field will ask the user to enter the first value and the second text field will ask also the user to enter the first value.

After which our program will give the user to select mathematical operations using the combo box in our web page their are for operations the addtion, subtraction, multiplication and division.If the user select addition the user can click the compute button which will perform the computation and display the results on the web page in the text field. 

This simple calculator program that I wrote is intended for beginners that are new in PHP programming. Based in my experience as a software developer and web developer in learning PHP you must have basic understanding and knowledge in HTML because HTML is the one will accept input values from the user of your program. It will follow the basic understanding how the syntax and commands in PHP making small programming will develop your skills and logic how the program works.

I hope you will find my work useful in your quest in learning PHP as your web scripting programming language.

Thank you very much until the next article.



Sample Output of our Program

Program Listing


<?php
  // Written By: Jake R. Pomperada
  // May 30, 2014 Friday
  // Tools : Wamp Server
  // Email Address: jakerpomperada@yahoo.com
  
  error_reporting(0);
  if ($_REQUEST['clear'])
   {
    
$val_1 ="";
$val_2 ="";
$result ="";
}

   
  if ($_REQUEST['submit']) 
     {
   
$choice = $_REQUEST['choice'];  
 if ($choice == "1")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] +  $_REQUEST['val_2'];

}
elseif ($choice == "2")
   {
    $val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] -  $_REQUEST['val_2'];

}
elseif ($choice == "3")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] *  $_REQUEST['val_2'];
}
elseif ($choice == "4")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
    $result = $_REQUEST['val_1'] /  $_REQUEST['val_2'];
 
  }

?>  
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div align="center">
  <p><font size="4"><strong>Calculator 1.0</strong></font></p>

<form name="form2" method="post" action="">
    <p align="left">Enter First Number 
      <input type="text" name="val_1" 
 value="<?php echo $val_1; ?>">
  </p>
    <div align="left">Enter Second Number 
      <input type="text" name="val_2" 
 value="<?php echo $val_2; ?>">
 
    </div>

    <p align="left"><strong>Math Operation</strong></p>
    <p align="left"><strong>
    <select name="choice">
<option value="1"> Addition</option>
<option value="2"> Subtration</option>
  <option value="3"> Multiplication</option>
  <option value="4"> Division</option>
</select>
      </strong></p>
     
    <div align="left"><strong>Result</strong> 
      <input type="text" name="result" 
  value="<?php echo $result ?>" readonly>
    </div>
   <input type="submit" name="submit" value="Compute">
    <tr> </tr>  <tr> </tr> 
   <input type="submit" name="clear" value="Clear">

  </form>
 <p>&nbsp;</p>
</div>
</body>
</html>