【问题标题】:Displaying the value calculated from textbox inputs显示从文本框输入计算的值
【发布时间】:2016-11-07 00:32:42
【问题描述】:

我应该创建一个 PHP 应用程序,让用户计算达到投资目标所需的投资金额。

有两种投资选择:

  1. 每月投资
  2. 一次性一次性投资。

在我提供的 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>&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;
} 
?>

【问题讨论】:

    标签: php html web-applications


    【解决方案1】:

    单选值应被视为数组。我对您的代码进行了一些小修改:

    1. $radio_value 应该是一个数组,所以我使用 $radio_value[0] 来获取第一个索引,因为您只是获得每月或一次性付款,并在输入类型元素上返回该值,以便用户知道他/她正在选择什么。

    2. 在使用函数时,调用函数应放入变量中,以便在需要时可以调用变量,您使用的变量与您的函数无关。

    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:

    我没有提到你的计算方式。 ^_^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多