Jump to content

[SOLVED] Mortgage Calculator Function HELP


SilentQ-noob-

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/64289-solved-mortgage-calculator-function-help/
Share on other sites

<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.

<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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.