Hello,
I am very new to PHP and am currently trying to emulate the windows calculator. I am trying to understand the notion of how to get a form button to communicate with PHP. For example, if a user presses the "5" button, I would like 5 to be appended onto the text field/display (if it is not the first digit, at which case the initial 0 that sits on the screen needs to be clared first). Here is what I have so far, the interface is built, and I have been trying to use the $_POST array then echo, but I cannot get the display value to change.
Thanks in advance,
Rafael.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<?php
$inputText = 0;
$isFirstInput = true; // Determines if the number being pressed is the first
//in the display ("0." has to be replaced with the number plus ".")
$isFirstOperand = true; // Determines if the operand being stored is the one
//to the left side of the operator
$operator = ""; // Will store the name of the operator upon onRelease of a #btn
$reg1 = 0; // Stores a value in the memory. It will be a number
$operand1 = 0; //Stores first value in an operation statement
$operand2 = 0; //Stores the second value in the operation
//$formatted = number_format($number, 2);
//During the second operation, the program needs to be aware that a first
//operand and an operator already exist
function secondOperand($isFirstOperand)
{
$isFirstOperand = false; // Defines that the number is the second operand
return $isFirstOperand;
}
function doOperation($operand1, $operand2, $operator)
{
if(is_numeric($inputText)) / /
{
if($operator != null) / Operator info stored here /
{
switch($operator)
{
case "btnAdd" : $result= $operand1 + $operand2; break;
case "btnSub" : $result= $operand1 - $operand2; break;
case "btnMult" : $result= $operand1 * $operand2; break;
case "btnDiv" : $result= $operand1 / $operand2; break;
}
echo("Calculation result: $result");
//For testing only, this needs to output to the text box
}
}
else
{
echo("Invalid entry - please retry with numbers only.");
}
}
?>
<html>
<head>
<title>Calculator</title>
<style>
/ All style information is embeded in this file in order to produce /
/ a single stand-alone file at the end of this project. /
.buttonSmall
{
width:45px; height:35px; margin:4px 0px 0px 0px;
}
.buttonLarge
{
width:78px; height:35px;
}
.display
{
margin-bottom:10px; width:284px; text-align:right;
}
#container
{
position:absolute; height:240px; width:300px;
background-color:#ccc; padding:20px 0px 0px 10px;
}
#topLeftSquare
{
position:relative; float:left; height:28px; width:30px;
background-color:#ccc; margin:3px 9px 0px 9px;
}
</style>
</head>
<body>
<div id="container">
<form action="$_SERVER['PHP_SELF']" method="post">
<input type="text" name="inputText" value="<?php echo $inputText; ?>" maxlength="35" class="display"><br/>
<div id="topLeftSquare"></div> <!-- Irrelevant, but present in the windows calc. -->
<input type="button" name="btnBackspace" value="Backspace" class="buttonLarge">
<input type="button" name="btnCe" value="CE" class="buttonLarge">
<input type="button" name="btnC" value="C" class="buttonLarge"><br/>
<input type="button" name="btnMc" value="MC" class="buttonSmall">
<input type="button" name="btnSeven" value="7" class="buttonSmall"
onClick="<?php $POST['inputText']= "7"; echo $POST['inputText']; ?>">
<input type="button" name="btnEight" value="8" class="buttonSmall">
<input type="button" name="btnNine" value="9" class="buttonSmall">
<input type="button" name="btnDiv" value="/" class="buttonSmall">
<input type="button" name="btnSquare" value="sqrt" class="buttonSmall"><br/>
<input type="button" name="btnMr" value="MR" class="buttonSmall">
<input type="button" name="btnFour" value="4" class="buttonSmall">
<input type="button" name="btnFive" value="5" class="buttonSmall">
<input type="button" name="btnSix" value="6" class="buttonSmall">
<input type="button" name="btnMult" value="*" class="buttonSmall">
<input type="button" name="btnPercent" value="%" class="buttonSmall"><br/>
<input type="button" name="btnMs" value="MS" class="buttonSmall">
<input type="button" name="btnOne" value="1" class="buttonSmall">
<input type="button" name="btnTwo" value="2" class="buttonSmall">
<input type="button" name="btnThree" value="3" class="buttonSmall">
<input type="button" name="btnSub" value="-" class="buttonSmall">
<input type="button" name="btnFrequency" value="1/x" class="buttonSmall"><br/>
<input type="button" name="btnMplus" value="M+" class="buttonSmall">
<input type="button" name="btnZero" value="0" class="buttonSmall">
<input type="button" name="btnSignChange" value="+/-" class="buttonSmall">
<input type="button" name="btnDecimal" value="." class="buttonSmall">
<input type="button" name="btnAdd" value="+" class="buttonSmall">
<input type="button" name="btnEqual" value="=" class="buttonSmall"><br/>
</div>
</form>
</body>
</html>