display_result.php
<?php
// get the data from the form
$investment = filter_input(INPUT_POST, 'investment',
FILTER_VALIDATE_FLOAT);
$interest_rate = filter_input(INPUT_POST, 'interest_rate',
FILTER_VALIDATE_FLOAT);
$years = filter_input(INPUT_POST, 'years',
FILTER_VALIDATE_INT);
// validate investment
if ($investment === FALSE )
{
$error_message = 'Investment must be a valid number.';
}
else if ( $investment <= 0 )
{
$error_message = 'Investment must be greater than zero.';
// validate interest rate
}
else if ( $interest_rate === FALSE )
{
$error_message = 'Interest rate must be a valid number.';
}
else if ( $interest_rate <= 0 )
{
$error_message = 'Interest rate must be greater than zero.';
// validate years
}
else if ( $interest_rate >= 15 )
{
$error_message = 'Interest rate must be less than or equal to 15.';
// validate years
}
else if ( $years === FALSE ) {
$error_message = 'Years must be a valid whole number.';
}
else if ( $years <= 0 )
{
$error_message = 'Years must be greater than zero.';
}
else if ( $years > 30 )
{
$error_message = 'Years must be less than 31.';
// set error message to empty string if no invalid entries
}
else
{
$error_message = '';
}
// if an error message exists, go to the index page
if ($error_message != '')
{
include('index1.php');
exit();
}
// calculate the future value
$future_value = $investment;
if (isset($_POST["monthly"]))
{ //compound monthly
$compounded_monthly="Yes";
$months=$years*12;
$monthly_rate = $interest_rate / 12;
for ($i=1;$i<=$months;$i++)
{
$future_value =$future_value + ($future_value * $monthly_rate * .01);
}
// apply currency and percent formatting
}
else
{
for ($i = 1; $i <= $years; $i++)
{
$future_value =$future_value + ($future_value * $interest_rate * .01);
}
}
$investment_f = '$'.number_format($investment, 2);
$yearly_rate_f = $interest_rate.'%';
$future_value_f = '$'.number_format($future_value, 2);
$monthly_rate_f=number_format($monthly_rate,2).'%';
$now = new DateTime();
?>
<!DOCTYPE html>
<html>
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Future Value Calculator</h1>
<label>Investment Amount:</label>
<span><?php echo $investment_f; ?></span><br>
<?php
if (isset($_POST["monthly"]))
{
echo "<label>Monthly Interest Rate:</label>";
echo "<span>".$monthly_rate_f."</span><br>";
echo" <label>Number of Months:</label>";
echo "<span>".$months."</span><br>";
echo" <label>Future Value:</label>";
echo"<span>".$future_value_f."</span><br>";
echo"Compound Monthly:".$compounded_monthly."</br>";
}
else
{
echo "<label>Yearly Interest Rate:</label>";
echo "<span>".$yearly_rate_f."</span><br>";
echo" <label>Number of Years:</label>";
echo "<span>".$years."</span><br>";
echo" <label>Future Value:</label>";
echo"<span>".$future_value_f."</span><br>";
}
?>
<label>This calculation was done on:<?php echo date("Y-m-d") ?></label>
</main>
</body>
</html>
main html file
<!DOCTYPE html>
<html>
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<main>
<h1>Future Value Calculator</h1>
<form action="display_results.php" method="post">
<div id="data">
<label>Investment Amount:</label>
<input type="text" name="investment">
<br>
<label>Yearly Interest Rate:</label>
<input type="text" name="interest_rate">
<br>
<label>Number of Years:</label>
<input type="text" name="years" >
<br>
<input type="checkbox" name="monthly" value="monthly"> Compound Interest Monthly <br>
</div>
<div id="buttons">
<label> </label>
<input type="submit" value="Calculate"><br>
</div>
</form>
</main>
</body>
</html>