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

时间:2016-11-07 00:32:42

标签: php html web-applications

我应该创建一个PHP应用程序,让用户计算他们为达到投资目标而需要投入的金额。

有两种投资选择:

  1. 每月投资
  2. 一次性一次性投资。
  3. 在我提供的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;
    } 
    ?>
    

1 个答案:

答案 0 :(得分:0)

无线电值应视为数组。我对您的代码进行了一些小修改:

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

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

  3. <强> 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:

    我没有触及你的计算方式。 ^ _ ^