Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A) Enhance the php file so it tests the interest rate entry to make sure it is a

ID: 3877086 • 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);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Future Value Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
    <div id="content">
        <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 />
    </div>
</body>
</html>

index file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Future Value Calculator</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
    <div id="content">
    <h1>Future Value Calculator</h1>
    <?php if (!empty($error_message)) { ?>
        <p class="error"><?php echo $error_message; ?></p>
    <?php } // end if ?>
    <form action="display_results.php" method="post">

        <div id="data">
            <label>Investment Amount:</label>
            <input type="text" name="investment"
                   value="<?php echo $investment; ?>"/><br />

            <label>Yearly Interest Rate:</label>
            <input type="text" name="interest_rate"
                   value="<?php echo $interest_rate; ?>"/><br />

            <label>Number of Years:</label>
            <input type="text" name="years"
                   value="<?php echo $years; ?>"/><br />
        </div>

        <div id="buttons">
            <label>&nbsp;</label>
            <input type="submit" value="Calculate"/><br />
        </div>

    </form>
    </div>
</body>
</html>

Explanation / Answer

$investment = $_POST['investment'];
$interest_rate = $_POST['interest_rate'];
$years = $_POST['years'];
   $now = new DateTime();
  

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 || $interest_rate >15 ) {
$error_message = 'Interest rate must be greater than zero and less than or equal to 15.'; }
   else if ( $years <= 0 || $years >50 ) {
$error_message = 'years must be greater than zero and less than or equal to 50.'; }

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);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
<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 />
       <span><?php echo $now->format('Y-m-d'); ?></span><br />
</div>
</body>
</html>
index file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Future Value Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
<h1>Future Value Calculator</h1>
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo $error_message; ?></p>
<?php } // end if ?>
<form action="display_results.php" method="post">
<div id="data">
<label>Investment Amount:</label>
<input type="text" name="investment"
value="<?php echo $investment; ?>"/><br />
<label>Yearly Interest Rate:</label>
<input type="text" name="interest_rate"
value="<?php echo $interest_rate; ?>"/><br />
<label>Number of Years:</label>
<input type="text" name="years"
value="<?php echo $years; ?>"/><br />
</div>
<div id="buttons">
<label>&nbsp;</label>
<input type="submit" value="Calculate"/><br />
</div>
       <div id="data">
           <label>Date:</label>
           <input value="<?php echo $now; ?>"/><br />
</div>
      
</form>
</div>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote