'Make Calculator - When Button clicked the No shows in Textbox.
Right guys, I am currently learning php, and before you lot say to use javascript instead i am learning php and want it with this code.
Basically i want to kind of replicate the calculator on windows 7, however just with the buttons 0-9, + - / * and =.
before you read the code, i have only built it so far to try and make it work something like this.
You click 1 then it would display on the texbox if you clicked 5 it would put 5 after 1 in the textbox etc... then when you click an operator such as divide(which is the only one ive tried to code so far as not proceeding as not working) the textbox deletes the numbers but saves the total figure and then any new numbers put in will be divided when the submit button is pressed.
however when i run my code and click any button in my web browser nothing goes into the text box. Any help would really be appreciated. (learning php so that i can progress from 1st line support to web development in same company so this is important)
code is below , i have been using xampp to run it
<html>
<head>
<title>Calculator</title>
<style>
#wrapper{
width:500px;
height:500px;
margin:auto;
border:1px solid;
}
#content{
width:500px;
height:500px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="content">
<?php
$answer = $display = $saveddisplay = $newdisplay = $add = $subtract = $divide = $multiply = $num0 = $num1 = $num2 = $num3 = $num4 = $num5 = $num6 = $num7 = $num8 = $num9 = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$num0 = ($_POST["number0"]);
$num1 = ($_POST["number1"]);
$num2 = ($_POST["number2"]);
$num3 = ($_POST["number3"]);
$num4 = ($_POST["number4"]);
$num5 = ($_POST["number5"]);
$num6 = ($_POST["number6"]);
$num7 = ($_POST["number7"]);
$num8 = ($_POST["number8"]);
$num9 = ($_POST["number9"]);
$add = ($_POST["add"]);
$subtract = ($_POST["subtract"]);
$divide = ($_POST["divide"]);
$multiply = ($_POST["multiply"]);
///////////////////////////////////////////NUMBERS
if($_POST["number0"]){
$display = $display && ($_POST["number0"]);
}
if($_POST["number1"]){
$display = $display && ($_POST["number1"]);
}
if($_POST["number2"]){
$display = $display && ($_POST["number2"]);
}
if($_POST["number3"]){
$display = $display && ($_POST["number3"]);
}
if($_POST["number4"]){
$display = $display && ($_POST["number4"]);
}
if($_POST["number5"]){
$display = $display && ($_POST["number5"]);
}
if($_POST["number6"]){
$display = $display && ($_POST["number6"]);
}
if($_POST["number7"]){
$display = $display && ($_POST["number7"]);
}
if($_POST["number8"]){
$display = $display && ($_POST["number8"]);
}
if($_POST["number9"]){
$display = $display && ($_POST["number9"]);
}
//////////////////////////////////////////OPERATORS
if($_POST["divide"]){
$saveddisplay = $display;
$display = "";
if($_POST["number0"]){
$newdisplay = $newdisplay && ($_POST["number0"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number1"]){
$newdisplay = $newdisplay && ($_POST["number1"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number2"]){
$newdisplay = $newdisplay && ($_POST["number2"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number3"]){
$newdisplay = $newdisplay && ($_POST["number3"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number4"]){
$newdisplay = $newdisplay && ($_POST["number4"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number5"]){
$newdisplay = $newdisplay && ($_POST["number5"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number6"]){
$newdisplay = $newdisplay && ($_POST["number6"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number7"]){
$newdisplay = $newdisplay && ($_POST["number7"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number8"]){
$newdisplay = $newdisplay && ($_POST["number8"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
if($_POST["number9"]){
$newdisplay = $newdisplay && ($_POST["number9"]);
if($_POST["submit"]){
$answer = $saveddisplay / $newdisplay && $newdisplay = "";
}
}
}
}
?>
<form method ="POST" action="<?php ($SERVER["PHP_SELF"]);?>">
<input type="text" width="50" height="20" name="display" value="<?php echo $display or $newdisplay or $answer;?>">
<br><br><br><br><br><br>
<button type="button" name="number7" value="7" style="width:20px; padding-left:5px; margin-left:2px;">7</button>
<button type="button" name="number8" value="8" style="width:20px; padding-left:5px; margin-left:2px;">8</button>
<button type="button" name="number9" value="9" style="width:20px; padding-left:5px; margin-left:2px;">9</button>
        
<button type="button" name="divide" value="/" style="width:20px; padding-left:5px; margin-left:2px;">/</button>
<button type="button" name="multiply" value="*" style="width:20px; padding-left:5px; margin-left:2px;">*</button>
<button type="button" name="subtract" value="-" style="width:20px; padding-left:5px; margin-left:2px;">-</button>
<button type="button" name="add" value="+" style="width:20px; padding-left:5px; margin-left:2px;">+</button>
<br><br>
<button type="button" name="number4" value="4" style="width:20px; padding-left:5px; margin-left:2px;">4</button>
<button type="button" name="number5" value="5" style="width:20px; padding-left:5px; margin-left:2px;">5</button>
<button type="button" name="number6" value="6" style="width:20px; padding-left:5px; margin-left:2px;">6</button>
<br><br>
<button type="button" name="number1" value="1" style="width:20px; padding-left:5px; margin-left:2px;">1</button>
<button type="button" name="number2" value="2" style="width:20px; padding-left:5px; margin-left:2px;">2</button>
<button type="button" name="number3" value="3" style="width:20px; padding-left:5px; margin-left:2px;">3</button>
<br><br>
<button type="button" name="number0" value="0" style="width:20px; padding-left:5px; margin-left:2px;">0</button>
<button type="submit" value="=" name="submit" style="width:46px; padding-left:5px; margin-left:2px;">=</button>
</form>
</div>
</div>
</body>
</html>
Solution 1:[1]
On php side, read the math expression using eval(); check this example:
<?php
$res = 0;
$mathExpression = "((2+2) /4) *5";
eval("\$res = $mathExpression;");
echo $res;
?>
$mathExpression gets evaluated and the result gets in $res.
and change this line:
<form method ="POST" action="<?php ($SERVER["PHP_SELF"]);?>">
for this:
<form method ="POST" action="<?php echo $_SERVER["PHP_SELF"];?>">
Solution 2:[2]
The data will not be submitted automatically when button type is set to button
. You need to set the type to submit
.
<button type="submit" name="number6" value="6" style="width:20px; padding-left:5px; margin-left:2px;">6</button>
If you want the numbers to be inserted into the input element before submitting the form with the equals button, you must do that with JavaScript.
Give your input element and id.
<input id='result' type="text" width="50" height="20" name="display" value="<?php echo $display or $newdisplay or $answer;?>">
Add an onClick handler to each button that populates the input field.
<button type="button" name="number6" value="6" onClick="document.getElementById('result').value += this.value">6</button>
You'll want to modify the handler a bit to get the desired effect but this should give you an idea.
Solution 3:[3]
What you are trying to do is completely insane. I advise you to try a different approach. PHP is not good to make a Windows calculator(Javascript would be better).
What you could do is create two input fields called number1 and number2 and then create four submit buttons: add,subtract,divide,multiply.
In your code you should first check what POST data is reaching you. Have you got all the fields filled in? Are they numbers? Which button has been clicked.
About your code:
in PHP you don't have to define your variables, so no: $answer = $display = $saveddisplay = $newdisplay.......
$display = $display && ($_POST["number0"]);
this makes no sense, it will evaluate to a boolean. So will this: $answer = $saveddisplay / $newdisplay && $newdisplay = "";
PHP doesnt not save the state of the variables between sessions so it won't know what these variables were when you clicked a button while ago.$display or $newdisplay or $answer
PHP isn't that smart, you should use ternary operator or make them into one variable.
PS. I recommend you to read some books about programming or watch tutorials on YT, It might look easy but making a calculator in PHP isn't that straight forward.
Sample Calculator in PHP:
<?php
$number1 = (int)($_POST['number1']);
$number2 = (int)($_POST['number2']);
$action = $_POST['action'];
if(!isset($number1)){
echo 'Missing first operand!';
}
else if(!isset($number2)){
echo 'Missing second operand!';
}
else if(!empty($action)){
if($action == "Add"){
$result = $number1 + $number2;
}
else if($action == "Subtract"){
$result = $number1 - $number2;
}
else if($action == "Multiply"){
$result = $number1 * $number2;
}
else if($action == "Divide"){
$result = $number2 != 0 ? $number1 / $number2 : "NaN";
}
if(isset($result)){
echo "Your result is: ".$result;
}
}
?>
<form action="" method="POST">
<input type="number" required="required" name="number1" />
<input type="number" required="required" name="number2" />
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Subtract" />
<input type="submit" name="action" value="Multiply" />
<input type="submit" name="action" value="Divide" />
</form>
Solution 4:[4]
Try this sample code. Maybe it will be helpful for you :) Use Javascript for onclick button, better approach IMO.
It will produce the results on the same page, you can link also.
Save this file with .php extension.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample Calculator</title>
<meta name="description" content="Simple Calculation Using Arithmetic Operators">
<meta name="author" content="AloNe ForeVer">
</head>
<body>
<body background="http://previews.123rf.com/images/iunewind/iunewind1403/iunewind140300097/26559245-Black-numbers-vector-background-with-copy-space-Stock-Vector.jpg">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><center><form name="calculator" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input style=" color: #0000ff ; font-size: 150%;" type="text" name="num1" placeholder="Input Number 1" >
<input style=" color: #0000ff ; font-size: 150%;" type="text" name="num2" placeholder="Input number 2" > <select style=" color: #3336FF; font-size: 150%;" name="action">
<option>+</option>
<option>-</option>
<option>/</option>
<option>*</option>
</select>
<input style=" color: #FF0000; font-size: 150%;" type="submit" name="submit" value="Calculate" ></form></center>
<center> <h1>
<?php
if(isset($_POST["submit"])){
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$action = $_POST["action"];
if ($action=="+") {
echo '<h1> <span style="color:#0000ff;">Addition</span>' . "</br>";
$add=$num1+$num2;
echo " <input type='text' value='$add' />";
}
if ($action=="-") {
echo '<h1> <span style="color:#FF0000;">Subtraction</span>' . "</br>";
$subtract=$num1-$num2;
echo "<input type='text' value='$subtract' />";
}
if ($action=="/") {
echo '<h1> <span style="color:#0000ff;">Division</span>' . "</br>";
$divide=$num1/$num2;
echo "<input type='text' value='$divide' />";
}
if ($action=="*") {
echo '<h1> <span style="color:#FF0000;">Multiplication</span>' . "</br>";
$multiply=$num1*$num2;
echo "<input type='text' value='$multiply' />";
}
}
?></center>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user2512574 |
Solution 2 | |
Solution 3 | |
Solution 4 |