Test the Future Value application 1. Make sure the Apache server is running. Then, start your browser and run the application in this directory: xampp\htdocs\ex_starts\ch02_ex2 To do that, you can use the index of exercise starts that you bookmarked in exercise 1-2 or you can use this URL: http://localhost/ex_starts/ch02_ex2/
Test the Future Value application 1. Make sure the Apache server is running. Then, start your browser and run the application in this directory: xampp\htdocs\ex_starts\ch02_ex2 To do that, you can use the index of exercise starts that you bookmarked in exercise 1-2 or you can use this URL: http://localhost/ex_starts/ch02_ex2/
2. Enter valid numbers in all three text boxes. For the first test run, keep these values simple like 100 for investment amount, 5 for yearly interest rate, and 10 for number of years. Then, click the Calculate button to display the results.
3. Click the Back button to return to the first page, enter invalid values in the first two text boxes, and click the Calculate button. Then, respond to the error message that’s displayed by entering valid data for the investment amount, re-entering invalid data for interest rate, and clicking the Calculate button. This time, you need to respond to the error message for the invalid interest rate entry.
Enhance the validity checking
4. Use your IDE or text editor to open the display_results.php and index.php files. Then, review the code for these files.
5. Switch to the display_results.php file and enhance the code so it tests the interest rate entry to make sure it is less than or equal to 15. If the rate isn’t valid, display an appropriate error message like Interest rate must be less than or equal to 15. Then, save your changes, and test this enhancement.
Add the date to the web page
6. Modify the display_results.php file so it uses embedded PHP to display the current date at the bottom of the web page like this: This calculation was done on 3/14/2014. To do this, use the date function in figure 2-10. Then, test this change. When you’ve got it working right, close the files.
display_results.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 ( $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);
?>
<!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>
<label>Yearly Interest Rate:</label>
<span><?php echo $yearly_rate_f; ?></span><br>
<label>Number of Years:</label>
<span><?php echo $years; ?></span><br>
<label>Future Value:</label>
<span><?php echo $future_value_f; ?></span><br>
</main>
</body>
</html>
index.php
<?php
//set default value of variables for initial page load
if (!isset($investment)) { $investment = ''; }
if (!isset($interest_rate)) { $interest_rate = ''; }
if (!isset($years)) { $years = ''; }
?>
<!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>
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo htmlspecialchars($error_message); ?></p>
<?php } ?>
<form action="display_results.php" method="post">
<div id="data">
<label>Investment Amount:</label>
<input type="text" name="investment"
value="<?php echo htmlspecialchars($investment); ?>">
<br>
<label>Yearly Interest Rate:</label>
<input type="text" name="interest_rate"
value="<?php echo htmlspecialchars($interest_rate); ?>">
<br>
<label>Number of Years:</label>
<input type="text" name="years"
value="<?php echo htmlspecialchars($years); ?>">
<br>
</div>
<div id="buttons">
<label> </label>
<input type="submit" value="Calculate"><br>
</div>
</form>
</main>
</body>
</html>
No hand written and with explanation answer
Trending now
This is a popular solution!
Step by step
Solved in 3 steps