【问题标题】:Getting error after calling function in index.php在 index.php 中调用函数后出现错误
【发布时间】:2017-03-04 19:46:43
【问题描述】:

我在 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>

计算器.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);
}
?>

【问题讨论】:

  • 你读过变量作用域吗?您的 $calculate 变量甚至没有在您的 index.php 文件中声明。在你的方法内部,它不会给你一个错误,因为它是一个参数。

标签: php html model-view-controller


【解决方案1】:

你需要从函数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;
}
?>

【讨论】:

    猜你喜欢
    • 2020-08-04
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多