10 Replies - 101520 Views - Last Post: 26 March 2018 - 04:03 AM Rate Topic: -----

#1 snasona   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   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • 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   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   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • 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   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
  • +
  • -

#6 oneAllegro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-March 18

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

Posted 25 March 2018 - 10:15 AM

View PostDormilich, on 12 October 2012 - 03:46 AM, said:

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



Im learning PHP and I was confused on how to do a calculator app with PHP. What I understand from your post is that its difficult to do it clean in pure PHP? Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 824
  • View blog
  • Posts: 3,336
  • Joined: 13-June 14

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

Posted 25 March 2018 - 11:00 AM

This thread is really old, but why would you go to the server [i]at all[/il]? There's not much point you can do all the work client-side (i.e. in Javascript or something that transpiles to it).
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

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

Posted 26 March 2018 - 02:00 AM

Quote

What I understand from your post is that its difficult to do it clean in pure PHP?

Not at all. You only need to have a sensible programme flow.
Was This Post Helpful? 0
  • +
  • -

#9 oneAllegro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-March 18

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

Posted 26 March 2018 - 02:53 AM

View PostDormilich, on 26 March 2018 - 02:00 AM, said:

Quote

What I understand from your post is that its difficult to do it clean in pure PHP?

Not at all. You only need to have a sensible programme flow.


Ok. But you will need to refresh the page at every step of the way, right?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

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

Posted 26 March 2018 - 03:03 AM

That's how server-side scripts work. And because of that you need to decide, if a server-side only solution is a sensible mode of operation.
Was This Post Helpful? 0
  • +
  • -

#11 oneAllegro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-March 18

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

Posted 26 March 2018 - 04:03 AM

View PostDormilich, on 26 March 2018 - 03:03 AM, said:

That's how server-side scripts work. And because of that you need to decide, if a server-side only solution is a sensible mode of operation.


Thanks! Yes, thats what I was thinking. I´m getting used now to understand better what approach is best suited.

This post has been edited by Dormilich: 26 March 2018 - 04:33 AM
Reason for edit:: removed duplicate

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1