helen11 Posted June 28, 2012 Share Posted June 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264960-php-calculator/ Share on other sites More sharing options...
Drummin Posted June 28, 2012 Share Posted June 28, 2012 I would suggest making each <select name="operator"> a unique name, operator1 operator2 etc and handle POST accordingly. OR only have ONE operator selection. Also you're not passing values for your options. <option value="+">+</option> Quote Link to comment https://forums.phpfreaks.com/topic/264960-php-calculator/#findComment-1357783 Share on other sites More sharing options...
Barand Posted June 28, 2012 Share Posted June 28, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264960-php-calculator/#findComment-1357792 Share on other sites More sharing options...
memfiss Posted June 29, 2012 Share Posted June 29, 2012 best practice forc calc on php is 1 input text , then with regxp remove non needed cymbols and eval() it Quote Link to comment https://forums.phpfreaks.com/topic/264960-php-calculator/#findComment-1357890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.