SilentQ-noob- Posted August 10, 2007 Share Posted August 10, 2007 Hi, I'm still pretty new to functions and haven't used them much. I wanted to make a function that takes user input data and calculates a monthly payment for a mortgage. I'm not sure if the actual syntax is wrong, or if it just comes down to the math- but its not working. this is the form: page1.php <form action="mortgage_calculate.php" name"MortgageCalculator"> <table border="0" width="400"> <caption> Mortgage Calculator </caption> <tr><td>List Price: <input type="text" value="listprice" /></td></tr> <tr><td>Rate: <input type="text" value="rate" /> %</td></tr> <tr><td>Downpayment: <input type="text" value="downpayment" /></td></tr> <tr><td>Years: <input type="text" value="25" /></td></tr> <tr><td><input type="submit" name="submit" value="Calculate" /></td></tr> </table> </form> This is the actual PHP mortgage_calculate.php <? $listprice=$_POST['listprice']; $rate=$_POST['rate']; $downpayment=$_POST['downpayment']; $years=$_POST['years']; function MortgageCalculator ($listprice, $rate, $downpayment, $years) { $monthly= (($listprice - $downpayment) * $rate ) / ($years * 12 ); return $monthly; } print "Your Monthly Payments Should be Approximately: "; print Mortgagecalculator($listprice, $rate, $downpayment, $years) ?> <p> Click <a href="page1.php">HERE</a> to go back to the calculator. Regardless of user input- it always calculates to zero, or most likely doesn't calculate at all once the "Calculate" button is hit. ANY help would be greatly appreciated. THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/ Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 <form action="mortgage_calculate.php" name"MortgageCalculator"> <table border="0" width="400"> <caption> Mortgage Calculator </caption> <tr><td>List Price: <input type="text" name="listprice" value="listprice" /></td></tr> <tr><td>Rate: <input type="text" name="rate" value="rate" /> %</td></tr> <tr><td>Downpayment: <input type="text" value="downpayment" /></td></tr> <tr><td>Years: <input type="text" name="years" value="25" /></td></tr> <tr><td><input type="submit" name="submit" value="Calculate" /></td></tr> </table> </form> it could be just your form not having the names. Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320487 Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 <form action="mortgage_calculate.php" name"MortgageCalculator"> <table border="0" width="400"> <caption> Mortgage Calculator </caption> <tr><td>List Price: <input type="text" name="listprice" value="listprice" /></td></tr> <tr><td>Rate: <input type="text" name="rate" value="rate" /> %</td></tr> <tr><td>Downpayment: <input type="text" value="downpayment" /></td></tr> <tr><td>Years: <input type="text" name="years" value="25" /></td></tr> <tr><td><input type="submit" name="submit" value="Calculate" /></td></tr> </table> </form> it could be just your form not having the names. sorry <form action="mortgage_calculate.php" name"MortgageCalculator"> <table border="0" width="400"> <caption> Mortgage Calculator </caption> <tr><td>List Price: <input type="text" name="listprice" value="listprice" /></td></tr> <tr><td>Rate: <input type="text" name="rate" value="rate" /> %</td></tr> <tr><td>Downpayment: <input type="text" name="downpayment" value="downpayment" /></td></tr> <tr><td>Years: <input type="text" name="years" value="25" /></td></tr> <tr><td><input type="submit" name="submit" value="Calculate" /></td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320488 Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 PHP: <? if($_POST['submit']) { $listprice=$_POST['listprice']; $rate=$_POST['rate']; $downpayment=$_POST['downpayment']; $years=$_POST['years']; print "Your Monthly Payments Should be Approximately: "; print Mortgagecalculator($listprice, $rate, $downpayment, $years); print "<br />Click <a href=\"page1.php\">HERE</a> to go back to the calculator."; } function MortgageCalculator($listprice, $rate, $downpayment, $years) { $monthly = (($listprice - $downpayment) * $rate ) / ($years * 12 ); return $monthly; } ?> FORM: <form action="" method="POST"> <table border="0" width="400"> <caption> Mortgage Calculator </caption> <tr><td>List Price: <input type="text" name="listprice" value="listprice" /></td></tr> <tr><td>Rate: <input type="text" name="rate" value="rate" /> %</td></tr> <tr><td>Downpayment: <input type="text" name="downpayment" value="downpayment" /></td></tr> <tr><td>Years: <input type="text" name="years" value="25" /></td></tr> <tr><td><input type="submit" name="submit" value="Calculate" /></td></tr> </table> </form> Forget about the last 2 and try this one, if this is not what your looking for then you should play with the math abit but this should work. Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320503 Share on other sites More sharing options...
MadTechie Posted August 10, 2007 Share Posted August 10, 2007 @Adam1239, you can EDIT your last post.. you don't need to post over and over! Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320507 Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 Also you will need to do a few checks on the inputs like is_numeric() etc because right now a user can enter eny letter/number they want and it may make it throw up errors with the function doing a calculation.. Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320510 Share on other sites More sharing options...
Adam1239 Posted August 10, 2007 Share Posted August 10, 2007 Thank you MadTechie, i'll keep that in mind next time Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320516 Share on other sites More sharing options...
SilentQ-noob- Posted August 10, 2007 Author Share Posted August 10, 2007 Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/#findComment-320522 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.