I was initially going to create a classic “updates while you type” Javascript based pension pot calculator but did not want the user to be able to see the business logic by downloading the .js file. However, there does not appear to be a robust way of protecting the business logic in the .js file.
Therefore, I am now going to put the calculation logic on the server in PHP. There is plenty of material out there with respect to creating a form which is submitted to another page that lists the outputs. However, my calculator is a side bar on the home page and I would like to user to click “calculate” and for the results to be displayed in the same box without being taken to a separate page.
Is this feasible and if so what would you suggest as the architecture of the solution? (e.g. keep calculation logic in .php and still call it via Javascript using xyz etc). Looking for guidance and design recommendations. The aim is a calculator with reasonably complex logic but no database lookups and most importantly the user must not be moved to a separate page.
Many thanks.
3rd August 2012
EDIT (17th December 2012): So four months later and I finally cracked it! Have a look at http://www.pencalc.org which demos what I was trying to build. Uses Javascript to asynchronously call a PHP based calculation. Thank you guys for all your help.
knittl and sachleen were pushing you in the right direction, but I feel like a more complete answer is needed.
If you don’t want to expose your business logic to your end-users, you can just use js/AJAX to automatically submit the form to a PHP script. The script then computes some value for the calculator, and then only prints the value (say, $20.45). You JS will eventually see the response from the PHP script. Once you have the value from the server, dump that value into the relevant field in the calculator. Now all the user can see is a bunch of JS which posts the user’s data to a PHP script.
However, I would have a look at JQuery, which helps in automating a lot of the work of AJAX and anything that involves manipulating the DOM.