I want to have a form on my website whereby people can enter a number of variables and click a calculate button. It then shows a number on the webpage. I can make the form but i don't know how the calculation works or how i can make it appear on the page. I have designed the form here then the calculation needs to be house price (determined by the chosen house type and location) multiplied by 0.1 less your deposit divided by 36 Answer should appear underneath "estimated payments". If you can help i would be eternally grateful and rep awarded.
If I were doing this I would suggest either going with pure javascript, or an ajax script that calls to a php script. Pure javascript would be easier, but would display your estimation algorithm to everyone. The ajax and php would be more secure in that aspect, but may take longer to report the estimate. Let me know which you want and if I have time I'll code it up.
Ok, I worked something out for you. It should give you a good start. It uses ajax to call a calculater.php file that does the calculation and then the ajax will show the results on the same page. I reduced the form to basically the house price and the deposit. But I think you can work around with it once you get the idea. Create an html file and put the following in it (maybe: estimate.html): <script type="text/javascript" language="javascript"> var http_request = false; function makePOSTRequest(url, parameters) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; } http_request.onreadystatechange = alertContents; http_request.open('POST', url, true); http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", parameters.length); http_request.setRequestHeader("Connection", "close"); http_request.send(parameters); } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { result = http_request.responseText; document.getElementById('estimationresult').innerHTML = result; } else { alert('There was a problem with the request.'); } } } function get(obj) { var poststr = "houseprice=" + encodeURI( document.getElementById("houseprice").value ) + "&deposit=" + encodeURI( document.getElementById("deposit").value ); makePOSTRequest('calculator.php', poststr); } </script> <form action="javascript:get(document.getElementById('myform'));" name="myform" id="myform"> <fieldset> <legend> <label for="price" accesskey="n">House price: </label> <input type="text" id="houseprice" name="houseprice" tabindex="4" value="" title="Price"></br> </legend> <p> <label for="deposit" accesskey="">Your deposit: </label> <input type="text" id="deposit" name="deposit" tabindex="5" value="" title="Deposit"></br> </p> </fieldset> <fieldset> <legend>Estimated Payments</legend> <label for="kludge"></label> <input type="button" name="button" value="Submit" onclick="javascript:get(this.parentNode);"> </fieldset> </form> <div id="estimationresult"></div> HTML: Now create the calculator.php in the same directory and put the following code: <?php $houseprice = $_POST['houseprice']; $deposit = $_POST['deposit']; echo ($houseprice*0.1)/36; ?> PHP: Now go to estimate.html and test it. You need to work around with the code to fit your needs. For example, do some checking for non valid entries in the calculator.php Good Luck
Right it is working ok at the moment How do i stop the result at 2 decimal places. I know i need to use toFixed(2) but i dont know where it fits in?
like Sudoku-Master said, use round. so instead of echo ($houseprice*0.1)/36; you would have: echo round(($houseprice*0.1)/36,2); or if you have changed your formula you can use something like: $result = ($houseprice*0.1)/36; echo round($result,2);