Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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