So, in this Calculator application I am using two input HTML textboxes for calculations, a button for the calculation and a drop down list for selecting an operator, and a button for the calculation. Feel free to modify for your own security purposes in validating inputs.
images from www.freeimages.co.uk
<?php
ini_set('display_errors',0);
if( isset( $_POST['calculate'] ))
{
$operator=$_POST['operator'];
if($operator=="+")
{
$var1 = $_POST['input1'];
$var2 = $_POST['input2'];
$result= $var1+$var2;
}
if($operator=="-")
{
$var1 = $_POST['input1'];
$var2 = $_POST['input2'];
$result= $var1-$var2;
}
if($operator=="*")
{
$var1 = $_POST['input1'];
$var2 = $_POST['input2'];
$result =$var1*$var2;
}
if($operator=="/")
{
$var1 = $_POST['input1'];
$var2 = $_POST['input2'];
$result= $var1/$var2;
}
if($_POST['input1']==NULL || $_REQUEST['input2']==NULL)
{
echo "<script language=javascript> alert(\"Some of the fields are empty.\");</script>";
}
}
?>
I put ini_set(‘display_errors’,0); above so that the “Notice: Undefined index: input1 and input2 ” will not be shown while running this code in your localhost
<form method="post">
<table style="background:#395796;width:320px;color:#FFF;padding:10px;">
<tr>
<td>Enter First Number</td>
<td colspan="1"><input name="input1" type="number" /></td>
</tr>
<tr>
<td>Select Operator</td>
<td>
<select name="operator" style="width: 100px">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
</td>
</tr>
<tr>
<td>Enter First Number</td>
<td><input name="input2" type="number" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="calculate" value="Calculate" /></td>
</tr>
<tr>
<td>Answer = </td>
<td ><?php echo $result;?></td>
</tr>
</table>
</form>
Disclaimer: The view and opinions expressed in this blog post are that of the author and do not in any way represents the agency or department he/she currently belongs to. Further, this information should not be interpreted as an endorsement of any specific provider, service or offering.