Question
Write the code that gets and displays the data entered by the user 1. Open the index.php file for this application and review the code.
Write the code that gets and displays the data entered by the user
1. Open the index.php file for this application and review the code. Note the names that are used for the three text boxes.
2. Modify the code so it uses drop-down lists instead of text boxes for the first two entries.
3. For the investment amount, the drop-down list should display values from 10,000 to 50,000 incremented by 5,000. To do this, you can use a for loop that creates the HTML for the drop-down list.
If you have any trouble with this, you may want to look ahead to the last example in figure 8-11 of the next chapter. Or, you can come back to this after you read chapter 8.
4. For the yearly interest rate, the drop-down list should display values from 4 to 12 incremented by .5.
5. Test the application to make sure it works correctly.
display_results.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 ( $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('index.php');
exit();
}
// calculate the future value
$future_value = $investment;
for ($i = 1; $i <= $years; $i++) {
$future_value = ($future_value + ($future_value * $interest_rate *.01));
}
// apply currency and percent formatting
$investment_f = '$'.number_format($investment, 2);
$yearly_rate_f = $interest_rate.'%';
$future_value_f = '$'.number_format($future_value, 2);
?>
Future Value Calculator
index.php
//set default value of variables for initial page load
if (!isset($investment)) { $investment = '10000'; }
if (!isset($interest_rate)) { $interest_rate = '5'; }
if (!isset($years)) { $years = '5'; }
?>
Future Value Calculator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started