Jump to content

php calculator


helen11

Recommended Posts

Hi I am trying to make a php calculator with radio buttons to choose options and plus 5  textboxes for entry of any 5 numbers. The numbers entered into the textboxes and thee radio button option should to be posted to a backend php program but I can't get it to work. If anyone can offer some help it would be greatly appreciated as i'm new to php and am having great difficulty getting my head around it. My code is:

 

<h1>Simple Calculator</h1>

<form name="myform" action="calculation.php" method="POST">
<h2>Please Select an Option</h2>
<div>
<input type="radio" name="calculator" value="Highest"> Highest<br>
<input type="radio" name="calculator" value="Lowest" checked> Lowest<br>
<input type="radio" name="calculator" value="Average"> Average<br>
<input type="radio" name="calculator" value="Sort in ascending order"> Sort in ascending order<br>
</div>
</form>

<h2>Insert five numbers in the form and hit submit button</h2>
<form action="calculation.php" method="post">
Enter First No.
<input type="text" name="firstnum">
Select operator:
<select name="operator">
  <option>+</option>
  <option>-</option>
  <option>*</option>
  <option>/</option>
</select>
Enter Second No.
<input type="text" name="secondnum">
Select operator:
<select name="operator">
  <option>+</option>
  <option>-</option>
  <option>*</option>
  <option>/</option>
</select>
Enter Third No.
<input type="text" name="thirdnum">
Select operator:
<select name="operator">
  <option>+</option>
  <option>-</option>
  <option>*</option>
  <option>/</option>
</select>
Enter Fourth No.
<input type="text" name="fourthnum">
Select operator:
<select name="operator">
  <option>+</option>
  <option>-</option>
  <option>*</option>
  <option>/</option>
</select>
Enter Fith No.
<input type="text" name="fithdnum">
<input type="submit" name="calculate" value="Calculate">
</form>
</body>
</html>

   

 

My php:

 

</head>
<body>
<?php
$first=$_POST['firstnum'];
$second=$_POST['secondnum'];
$third=$_POST['thirdnum'];
$fourth=$_POST['fourthnum'];
$fith=$_POST['fithnum'];
$operator=$_POST['operator'];
if ($operator=="+")
  {$total=$first + $second + $third + $fourth + $fith;}
if ($operator=="-")
  {$total=$first - $second - $third - $fourth - $fith;}
if ($operator=="*")
  {$total=$first * $second * $third * $fourth * $fith;}
if ($operator=="/")
  {$total=$first / $second / $third / $fourth / $fith;}
echo $total;
?>
</body>
</html>

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/264960-php-calculator/
Share on other sites

I'd call them all "operator[]" and similarly with the value input.

 

You can the reference them as $_POST['operator'][0] ... $_POST['operator['3'].

 

You will also need to take operator precedence into account. Multiplication and division before addition and subtraction ie 3+4*5 = 60, not 35

Link to comment
https://forums.phpfreaks.com/topic/264960-php-calculator/#findComment-1357792
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.