Php code for Basic Calculator

Php code for Basic Calculator

Welcome to this code of…… If you have any difficulty regarding this code, please like our page for discussions. Click here to like the page.

Php code for Basic Calculator


File name:Calculator

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>
<html xmlns=””>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
<style type=”text/css”>
.btn {color: #0000CC; border: #FF0000 1px solid; width:150px; height:35px; font-family:Verdana, Arial, Helvetica, sans-serif;
.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; }

<script type=”text/JavaScript”>
function MM_callJS(jsStr) { //v2.0
return eval(jsStr)

<body >
<form action=”” method=”post” name=”form1″ id=”form1″ >
//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
$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’) {
elseif ($operation == ‘sub’) {
elseif ($operation == ‘mul’) {
elseif ($operation == ‘div’)
// 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

//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;”>
<th colspan=”3″ bgcolor=”#0066FF” scope=”col” class=”style14″><h1 class=”h1″>Basic Calculator in PHP</h1>
<p class=”h1″>Created By: Mr. Jake R. Pomperada, MAED-IT </p></th>
</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–>
<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 class=”tr”>
<td height=”24″><div align=”right” class=”label”>Operator</div></td>
<td class=”style3″>:</td>
<td >
<span class=”style12″>
<input name=”operatorbutton” type=”radio” checked=”checked” <?php print $add; ?>; value=”add” />

<input name=”operatorbutton” type=”radio” value=”sub” <?php print $subtract; ?> />
<input name=”operatorbutton” type=”radio” value=”mul” <?php print $multiply; ?>/>
<input name=”operatorbutton” type=”radio” value=”div” <?php print $divide; ?> />
Division</span><span class=”style14″>&nbsp;</span></td>
<td height=”26″><div align=”right” class=”label”>Result</div></td>
<td class=”style3″>:</td>
<td class=”style3″><?php print $Fresult; ?></td>
<td class=”style4″><div align=”center”></div>
<div align=”center”></div></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” />
<td colspan=”3″><div align=”center” class=”tr1″></div></td>

<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>

Leave a Reply

Your email address will not be published. Required fields are marked *