Lately I have been designing and coding a website for one of my clients. One of the functions he needs is a calculator in PHP. The problem is, I can get the calculator up and running and I could probably make the calculator in jQuery as well, but he wants the result of the calculation on the next page. The calculator should be able to do simple calculations that include: multiplication, dividing, subtracting and adding. So far I have styled up and made the HTML structure of the calculator, but when it comes to the PHP code… I have no clue. So is there anyone here that has any experience of coding a calculator but showing its result in on a different page?
Preferably, you could use the HTML structure I have built:
<input type="text" title="result" class="input-result" name="result" readonly="readonly" value=""/>
<div class="row-container">
<ul class="row1-ul">
<li class="row1 calc-1 numbers">1</li>
<li class="row1 calc-2 numbers">2</li>
<li class="row1 calc-3 numbers">3</li>
<li class="row1 calc-x">X</li>
</ul>
<ul class="row2-ul">
<li class="row2 calc-4 numbers">4</li>
<li class="row2 calc-5 numbers">5</li>
<li class="row2 calc-6 numbers">6</li>
<li class="row2 calc-/">/</li>
</ul>
<ul class="row3-ul">
<li class="row3 calc-7 numbers">7</li>
<li class="row3 calc-8 numbers">8</li>
<li class="row3 calc-9 numbers">9</li>
</ul>
<ul class="row4-ul">
<li class="row4 calc-0 numbers">0</li>
<li class="row4 calc-+">+</li>
<li class="row4 calc--">-</li>
</ul>
<div class="column-last calc-=">=</div>
</div>
Since you say you could probably do it in jQuery, that would be the best idea. Wrap the whole calculator in something like this
and have a button enter the final result into that hidden input before performing
form.submit()(which is what an input of type submit does by standard). Your PHP script in “result.php” will be able to access the result as$_POST['finalresult']. Remember this comes from a script so it’s user input and can potentially be anything they are trying to do to wreck your scripts though.Edit:
$_POST['finalresult']will be a variable which you can use in the PHP script of results.php. Enter the result into the input using.val(num)and in results.php do something likevar $calc_result = (float) $_POST['finalresult'];to avoid users fiddling around.You will need at least the basics of PHP so I suggest you have a read through the w3 PHP tutorials: http://www.w3schools.com/php/php_syntax.asp (also see the $_POST one in the side bar).