A) Enhance the php file so it tests the interest rate entry to make sure it is a
ID: 3876670 • Letter: A
Question
A) Enhance the php file so it tests the interest rate entry to make sure it is also less than or equal to 15. To do that, use an OR operator. If the rate isn't valid, display an error message reading "rate must be greater than zero and less than or equal to 15."
B) Modify the php file so it tests the years entry to make sure it is a valid number that's greater than zero and less than or equal to 50. if it isn't, display an error message like the first one.
C) modify the 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 01/14/2018."
PHP file:
$investment = $_POST['investment'];
$interest_rate = $_POST['interest_rate'];
$years = $_POST['years'];
if ( empty($investment) ) {
$error_message = 'Investment is a required field.'; }
else if ( !is_numeric($investment) ) {
$error_message = 'Investment must be a valid number.'; }
else if ( $investment <= 0 ) {
$error_message = 'Investment must be greater than zero.'; }
else if ( empty($interest_rate) ) {
$error_message = 'Interest rate is a required field.'; }
else if ( !is_numeric($interest_rate) ) {
$error_message = 'Interest rate must be a valid number.'; }
else if ( $interest_rate <= 0 ) {
$error_message = 'Interest rate must be greater than zero.'; }
else {
$error_message = ''; }
if ($error_message != '') {
include('index.php');
exit();
}
$future_value = $investment;
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);
?>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Future Value Calculator
Future Value Calculator
Investment Amount:
Yearly Interest Rate:
Number of Years:
Future Value:
index file:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Future Value Calculator
Future Value Calculator
Investment Amount:
value="
"/>
Yearly Interest Rate:
value=""/>
Number of Years:
value=""/>
Explanation / Answer
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$iaerr = $yirerr = $noyerr = $fverr = "";
$ia = $yir = $noy = $fv ="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["ia"])) {
$iaerr = "amount is required";
}
else
{
$ia = test_input($_POST["ia"]);
}
if (empty($_POST["yir"])) {
$yirerr = "rate is required";
}elseif ($_POST["ia"]>15) {
$yirerr = "rate must be greater than zero and less than or equal to 15.";
}
else {
$yir = test_input($_POST["yir"]);
}
if (empty($_POST["noy"])) {
$noyerr = "year is required";
} elseif($_POST["noy"]<0 && $_POST["noy"]>50)
{
$noyerr ="year must be greater than zero and less than or equal to 50.";
}
else {
$noy = test_input($_POST["noy"]);
}
if (empty($_POST["fv"])) {
$fverr = "value is required";
} else {
$fv = test_input($_POST["fv"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>Investment Amount:</td>
<td> <input type="text" name="ia"><span class="error">* <?php echo $iaerr;?></span></td>
</tr>
<tr>
<td>Yearly Interest Rate:</td>
<td><input type="text" name="yir"><span class="error">* <?php echo $yirerr;?></span></td>
</tr>
<tr>
<td>Number of Years:</td>
<td><input type="text" name="noy"><span class="error">* <?php echo $noyerr;?></span></td>
</tr>
<tr>
<td>Future Value:</td>
<td><input type="text" name="fv"><span class="error">* <?php echo $fverr;?></span></td>
</tr>
</table>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $ia;
echo "<br>";
echo $yir;
echo "<br>";
echo $noy;
echo "<br>";
echo $fv;
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.