【发布时间】:2016-11-07 00:32:42
【问题描述】:
我应该创建一个 PHP 应用程序,让用户计算达到投资目标所需的投资金额。
有两种投资选择:
- 每月投资
- 一次性一次性投资。
在我提供的 HTML 模板中,有两个单选按钮来决定选择了哪个投资选项。根据检查哪一项,为每一项实施一个公式。
该公式通过从三个文本框。问题是当我点击提交按钮时,我一直得到值“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>
<input type="text" name="futureamount" value="" />
</p>
<p>
<label>Investment term/period (years):</label>
<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" />
<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;
}
?>
【问题讨论】:
标签: php html web-applications