显示从文本框输入计算的值

I am supposed to create a PHP application that lets the user calculate the amount they need to invest to reach an investment goal.

There are two investment options:

  1. A monthly investment
  2. A one time lump sum investment.

In the HTML template I was provided, there are two radio buttons to decide which investment option has been chosen. Depending on which one is checked, a formula is implemented for each one.

The formula calculates the required investment amount by extracting the Monthly Interest Rate, Future Investment Goal and the Time (Number of Months) from three text boxes. The problem is that when I hit the submit button, I keep getting the value "0".

HTML

<form action="<?php $_SERVER['PHP_SELF']?>" method="POST">
  <fieldset>
    <legend id="Investment Strategy">Investment Strategy:</legend>
    <input type="radio" name="investmentStrategy" value="lumpsum" />Lump sum Investment ($)
    <br />
    <input type="radio" name="investmentStrategy" value="monthly" />Monthly Investment ($)
    <br />

  </fieldset>
  <br />
  <fieldset>

    <legend id="Investment Goal">Investment Goal:</legend>

    <p>
      <label>Desired future amount ($):</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
      <input type="text" name="futureamount" value="" />
    </p>

    <p>
      <label>Investment term/period (years):</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
      <input type="text" name="term" value="" />
    </p>

    <p>
      <label>Desired rate of return/interest (%/year):</label>
      <input type="text" name="interest" value="" />
    </p>
  </fieldset>

  <p>
    <input type="reset" name="reset" value="Reset" />&nbsp;
    <input type="submit" name="submit" value="Calculate Amount to Invest" />
  </p>
</form>
<br />

PHP

<?php
$investmentValue = 0;
$investmentTerm = 0;
$investmentInterest = 0;
$monthlyInvestment = 0;
$lumpsumInvestment = 0;
$interestRate = 0;
$futureValue = 0;
$num_of_months = 0;


// Define functions and implement formula
function ComputeMonthlyPayment($interestRate, $futureValue, $num_of_months){
    $monthlyInvestment = ($interestRate * $futureValue) / (pow(1+$interestRate, $num_of_months)-1);
    return $monthlyInvestment;
}

function ComputeLumpsumPayment($interestRate, $futureValue, $num_of_months){
    $lumpsumInvestment = ($interestRate * $futureValue) / (pow(1+$interestRate, $num_of_months));
    return $lumpsumInvestment;
}

if(isset ($_POST['submit'])){

    $radio_value = $_POST['investmentStrategy'];

    if((isset($radio_value) && $radio_value=="lumpsum")){
        // Extract inputs and call function
        $investmentValue = @$_POST['futureamount'];
        $investmentTerm = @$_POST['term'];
        $investmentInterest = @$_POST['interest'];

        // Call function
        ComputeLumpsumPayment($investmentInterest, $investmentValue, $investmentTerm);

        // Display function
        echo $lumpsumInvestment;

    } elseif((isset($radio_value) && $radio_value == "monthly")) {
        $investmentValue = @$_POST['futureamount'];
        $investmentTerm = @$_POST['term'];
        $investmentInterest = @$_POST['interest'];

        // Call function
        ComputeMonthlyPayment($investmentInterest, $investmentValue, $investmentTerm);

        // Display investment
        echo $monthlyInvestment;
    }
} else {
    echo $test;
} 
?>

Radio values should be treated as array. I have some minor revisions on your codes:

  1. $radio_value should be an array, so I used $radio_value[0] to get the first index since you're just getting either monthly or lumpsum, and return that value on the input type element so that the user is aware on what he/she is selecting.

  2. When working with functions, calling functions should be put into variable so that variable can be called when needed, the variable you used is not associated with your function.

PHP

     // Define functions and implement formula
     function ComputeMonthlyPayment($interestRate, $futureValue, $num_of_months){
        $monthlyInvestment = ($interestRate * $futureValue) / (pow(1+$interestRate, $num_of_months)-1);
        return $monthlyInvestment;
     }

     function ComputeLumpsumPayment($interestRate, $futureValue, $num_of_months){
        $lumpsumInvestment = ($interestRate * $futureValue) / (pow(1+$interestRate, $num_of_months));
        return $lumpsumInvestment;
     }

     if(isset ($_POST['submit'])){

         if(isset($_POST['investmentStrategy'])) {
             $radio_value = $_POST['investmentStrategy'];
             $futureamount = $_POST['futureamount'];
             $term = $_POST['term'];
             $interest = $_POST['interest'];
         }


         if((isset($radio_value[0]) && $radio_value[0] =="lumpsum")){
             // Extract inputs and call function
             $investmentValue = $futureamount;
             $investmentTerm = $term;
             $investmentInterest = $interest;

             // Call function
             $lumpsumInvestment = ComputeLumpsumPayment($investmentInterest, $investmentValue, $investmentTerm);

             // Display function
             echo $lumpsumInvestment;

         } elseif((isset($radio_value[0]) && $radio_value[0] == "monthly")) {
             $investmentValue = $futureamount;
             $investmentTerm = $term;
             $investmentInterest = $interest;

             // Call function
             $monthlyInvestment = ComputeMonthlyPayment($investmentInterest, $investmentValue, $investmentTerm);

             // Display investment
             echo $monthlyInvestment;
         }
    } else {
         echo $test;
    } 
?>

HTML

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
      <fieldset>
      <legend id="Investment Strategy">Investment Strategy:</legend>
          <input type="radio" name="investmentStrategy[]" value="lumpsum" <?php echo $radio_value[0] == "lumpsum" ? "CHECKED" : ""; ?>  />Lump sum Investment ($)
       <br />
          <input type="radio" name="investmentStrategy[]" value="monthly" <?php echo $radio_value[0] == "monthly" ? "CHECKED" : ""; ?> />Monthly Investment ($)
       <br />

      </fieldset>
      <br />
      <fieldset>

         <legend id="Investment Goal">Investment Goal:</legend>

         <p>
         <label>Desired future amount ($):</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
  <input type="text" name="futureamount" value="<?php echo $futureamount; ?>" />
</p>

<p>
  <label>Investment term/period (years):</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
  <input type="text" name="term" value="<?php echo $term; ?>" />
</p>

<p>
  <label>Desired rate of return/interest (%/year):</label>
  <input type="text" name="interest" value="<?php echo $interest; ?>" />
</p>

 

PS:

I did not touch on the way how you calculate. ^_^