在index.php中调用函数后出错

时间:2017-03-04 19:46:43

标签: php html model-view-controller

我在MVC程序中调用函数时遇到错误。

Notice: Undefined variable: calculate in D:\xampp\htdocs\ch07en\future_value_calculator\index.php on line 26

Notice: Undefined variable: calculate in D:\xampp\htdocs\ch07en\future_value_calculator\index.php on line 27

我知道这个功能在我的模型中有效,其他一切都很完美。当我拨打calculate_investment($calculate);时,我会收到通知,但是当我不打电话时,该程序无法计算用户放入字段的所有内容。我究竟做错了什么?我在哪里需要定义变量?

以下是我的文件:

的index.php

   <?php
include('../model/calculator.php');

$action = filter_input(INPUT_POST, 'action');
if ($action == NULL) {
    $action = filter_input(INPUT_GET, 'action');
    if ($action == NULL) {
        $action = 'future_value_calculator';
    }
}

//Display future value calculator
if ($action == 'future_value_calculator') {
    $investment = filter_input(INPUT_POST, 'investment', FILTER_VALIDATE_FLOAT);
    $years = filter_input(INPUT_POST, 'years', FILTER_VALIDATE_INT);
    include('mainCalculator.php');

//When user presses calculate, calculate the FV    
} else if ($action == 'calculate') {
    $investment_f = filter_input(INPUT_POST, 'investment', FILTER_VALIDATE_FLOAT);
    $interest_rate = filter_input(INPUT_POST, 'interest_rate', FILTER_VALIDATE_FLOAT);
    $years = filter_input(INPUT_POST, 'years', FILTER_VALIDATE_INT);
    $future_value_f = filter_input(INPUT_POST, 'future_value_f', FILTER_VALIDATE_INT);

    if ($years == NULL) {
        $error = 'Enter a number in the years field and try again.';
        include('../errors/error.php');
    } else {
        $calculate = calculate_investment($calculate);
        if ($calculate) {
            include('displayResults.php');
        }
    }
}
?>

displayResults.php

<!DOCTYPE html>
<html>
<body>
    <main>
        <h1>Future Value Calculator</h1>

        <label>Investment Amount:</label>
        <span><?php echo $investment_f; ?></span><br>

        <label>Yearly Interest Rate:</label>
        <span><?php echo $interest_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>

        <label>Compound Interest Box Checked?</label>
        <span><?php echo $future_value_f; ?></span><br>

        <label>Compound Monthly:</label>
        <span><?php echo $future_value_f; ?></span><br>
    </main>
</body>
</html>

Calculator.php中

<?php
function calculate_investment($calculate){
        // get the data from the form

    $investment = filter_input(INPUT_POST, 'investment',  FILTER_VALIDATE_FLOAT);
    $interest_rate = filter_input(INPUT_POST, 'interest_rate', FILTER_VALIDATE_FLOAT);
    $investment_f = filter_input(INPUT_POST, 'interest_f', FILTER_VALIDATE_FLOAT);
    $yearly_rate_f = filter_input(INPUT_POST, 'yearly_rate_f ', FILTER_VALIDATE_INT);
    $years = filter_input(INPUT_POST, 'years', FILTER_VALIDATE_INT);
    $future_value_f = filter_input(INPUT_POST, 'future_value_f', FILTER_VALIDATE_INT);
    $interest = filter_input(INPUT_POST, 'interest', FILTER_VALIDATE_INT);




    // calculate the future value
    $future_value = $investment;
    for ($i = 1; $i <= $years; $i++) {
        $future_value = ($future_value + ($future_value * $interest_rate *.01));
    }

    // apply currency and percent formatting
    $investment_f = '$'.number_format($investment, 2);
    $yearly_rate_f = $interest_rate.'%';
    $future_value_f = '$'.number_format($future_value, 2);
}
?>

1 个答案:

答案 0 :(得分:0)

您需要从函数calculate_investment返回一些值。

例如:

<?php
function calculate_investment($calculate){
        // get the data from the form

    $investment = filter_input(INPUT_POST, 'investment',  FILTER_VALIDATE_FLOAT);
    $interest_rate = filter_input(INPUT_POST, 'interest_rate', FILTER_VALIDATE_FLOAT);
    $investment_f = filter_input(INPUT_POST, 'interest_f', FILTER_VALIDATE_FLOAT);
    $yearly_rate_f = filter_input(INPUT_POST, 'yearly_rate_f ', FILTER_VALIDATE_INT);
    $years = filter_input(INPUT_POST, 'years', FILTER_VALIDATE_INT);
    $future_value_f = filter_input(INPUT_POST, 'future_value_f', FILTER_VALIDATE_INT);
    $interest = filter_input(INPUT_POST, 'interest', FILTER_VALIDATE_INT);




    // calculate the future value
    $future_value = $investment;
    for ($i = 1; $i <= $years; $i++) {
        $future_value = ($future_value + ($future_value * $interest_rate *.01));
    }

    // apply currency and percent formatting
    $investment_f = '$'.number_format($investment, 2);
    $yearly_rate_f = $interest_rate.'%';
    $future_value_f = '$'.number_format($future_value, 2);
    return $future_value_f;
}
?>