4 Replies - 90731 Views - Last Post: 23 October 2017 - 09:12 PM Rate Topic: -----

#1 snasona  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 12

how to make complete calculator with all buttons using PHP?

Posted 12 October 2012 - 03:23 AM

 for calculator.php
<html>
<head>
<title>CALCULATOR USING PHP</title>	
<style type="text/css">              /*ADDING BACKGROUNG COLOUR ANS SETTINGS*/
    body{
	background-color: #d2b48c;
	margin-left: 20%;
	margin-right:20%;
	border: 1px dotted gray;
	font-family: sans-serif;
        }
</style>
	
<head>
<body>

<center></br></br>

<form name="frm" action="form1.php" method="post"> 
 Enter Value: 
<input type="text" name="first" value= "<?php echo $_REQUEST['number']; 
 echo $_REQUEST['calculate'];echo $_REQUEST['result']; ?>"> </br></br>
</from>


<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="1">
</from>

<form name="frm" action="form1.php" method="post">
 <input type="submit" name="number" value="2">
 </from>
 
 <form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="3"></br></br>
</from>
<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="4">
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="5">
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="6"></br></br>
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="7">
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="8">
</from>


<form name="frm" action="form1.php" method="post">
<input type="submit" name="number" value="9"></br></br>
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="calculate" value="+">
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="calculate" value="0">
</from>

<form name="frm" action="form1.php" method="post">
<input type="submit" name="calculate" value="*"></br></br>
</from>
<form name="frm" action="form1.php" method="post">
<input type="submit" name="calculate" value="/">
</from>


<form name="frm" action="form1.php" method="post">
<input type="submit" name="calculate" value="-"></br></br>
</form>

<form name="frm" action="form1.php" method="post">
<input type="submit" value="=" name="result">

</form>

</center>
</body>
</html>




 for form1.php


<html>
<head>
<title></title>
</head>
<body>

<?php


 			if(isset($_POST['result']))
		   {
		   include "at.php";
		     $oper=$_POST['calculate'];
		     
		    if($_oper == '+')
			{	
				echo "+";
				$oper='+';
				$result=$_POST['number']+10;
				echo $result;
			   
			}
		 else if($oper == '-')
			{
					echo "-";
					$oper='-';
					$result=$_POST['number']-10;
					echo $result;
					
			}
			else if($_POST['calculate'] == '/' )
			{
					echo "/";
					$oper='/';
					$result=$_POST['number']/10;
					echo $result;
				
			}
			else 
	
				{
					echo "*";
					$oper='*';
					$result=$_POST['number']*10;
					echo $result;
				}
			}
	

		if(isset($_POST['number']))
			{
			
		    include "at.php";
		    $var=$_POST['number']; 
 			
 			echo "</br>";
			echo  $var;
			echo "Enter the operator";
			}
  
  		if(isset($_POST['calculate']))
			{
			include "at.php";
		    $oper=$_POST['calculate'];
			
			echo $_POST['calculate'];
			echo "</br>";
			
 			}
 			
 						
			?>
		
</body>
</html>


This post has been edited by Dormilich: 12 October 2012 - 03:29 AM
Reason for edit:: removed unnecessary quote


Is This A Good Question/Topic? 0
  • +

Replies To: how to make complete calculator with all buttons using PHP?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,056
  • Joined: 08-June 10

Re: how to make complete calculator with all buttons using PHP?

Posted 12 October 2012 - 03:46 AM

from a design point, the approach is awful. why would I (as a user) need to reload the page on each click, even if I didnít even finish inputting the first number? Iíd probably give up after one or two clicks (esp. if my connection is not super-fast).

then there are flaws in the logic.

first you donít check, whether the POST variable you access is available at all. this would cause a couple of notices, if error reporting were turned on.

second, as soon as you hit the first button, you leave the calculator. and since form1.php (resp. its output) does not contain form code (donít know whatís inside at.php), thatís the end of the calculator.

third, if you hit the result button, it would print the asterisk (reason see point 1).

if you were to ask me, either use Javascript (AJAX) for a sufficient user experience or create a form that allows you to put in the equation at once (1st operand, operator and 2nd operand (at least)).
Was This Post Helpful? 1
  • +
  • -

#3 bparker1084  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 12

Re: how to make complete calculator with all buttons using PHP?

Posted 16 October 2012 - 02:31 AM

How about coming up with an original code that takes a single-line of input, without using pre-built PHP evaluation functions like "eval"?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,056
  • Joined: 08-June 10

Re: how to make complete calculator with all buttons using PHP?

Posted 16 October 2012 - 02:41 AM

then you would have to parse the expression and calculate it according to the result.

always bear in mind that eval() will execute a string as PHP command. even a single exploit you didnít think of may corrupt all your server and/or data! so in the end you would have to check the passed string for correctness, which is almost as complicated as parsing the string (with the additional feat that you still could have an opening). you would not gain much of an improvement when using eval() while retaining the risk.
Was This Post Helpful? 0
  • +
  • -

#5 1214703  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-October 17

Re: how to make complete calculator with all buttons using PHP?

Posted 23 October 2017 - 09:12 PM

View PostDormilich, on 16 October 2012 - 02:41 AM, said:

then you would have to parse the expression and calculate it according to the result.

always bear in mind that eval() will execute a string as PHP command. even a single exploit you didnít think of may corrupt all your server and/or data! so in the end you would have to check the passed string for correctness, which is almost as complicated as parsing the string (with the additional feat that you still could have an opening). you would not gain much of an improvement when using eval() while retaining the risk.


May see the complete code for php calculator please
thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1