You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter BMI计算器无法运行,求问题排查与修复方案

Fixing Your CodeIgniter BMI Calculator Issues

Let's walk through the errors you're seeing and fix your calculator step by step:

The Errors Explained

  • Undefined index: submit: Your submit button doesn't have a name attribute, so $_GET['submit'] doesn't exist when the form is submitted. Also, your form uses the default GET method, but we should switch to POST (more secure, and aligns with CodeIgniter's best practices).
  • Undefined variable: weight/height: When the page loads for the first time (before form submission), you're trying to use $weight and $height variables that haven't been defined yet.
  • Division by zero: Same as above—on initial page load, $height is undefined, so $height * $height evaluates to 0, causing a division by zero when calculating BMI.

Fixed View Code

<div id="container"> 
    <h1>BMI and Blood Pressure Calculator</h1> 
    <div id="body"> 
        <p>This webpage allows users to calculate their BMI and Blood Pressure by entering in their values into the boxes</p> 
        </br> 
        <p>Please enter in your details below (use meters for height, kilograms for weight)</p>
        <!-- Add method="post" and action to submit to same page -->
        <form method="post" action=""> 
            <br /> 
            <br />Patient Information 
            <br />Height (m) <input type="text" name="height" size="12" maxlength="20" value="<?php echo $this->input->post('height') ?? ''; ?>"> 
            <br />Weight (kg) <input type="text" name="weight" size="12" maxlength="20" value="<?php echo $this->input->post('weight') ?? ''; ?>"> 
            <br /> 
            <br /><input type="reset" value="Clear Form"> 
            <!-- Add name="submit" to detect form submission -->
            <br /><input type="submit" name="submit" value="Submit Information"> 
        </form> 
    </div> 

    <?php
    // Only run BMI logic when form is submitted
    if ($this->input->post('submit')) {
        // Get and clean up POST values
        $height = trim($this->input->post('height'));
        $weight = trim($this->input->post('weight'));

        // Validate inputs to avoid invalid calculations
        if (!empty($height) && !empty($weight) && is_numeric($height) && is_numeric($weight) && $height > 0) {
            function bmi($height, $weight) {
                return $weight / ($height * $height);
            }

            $bmi = bmi($height, $weight);
            $bmi = round($bmi, 2); // Round for readability

            // Determine BMI category
            if ($bmi <= 15) {
                $output = "Very severely underweight";
            } elseif ($bmi > 15 && $bmi <= 16 ) {
                $output = "Severely underweight";
            } elseif ($bmi > 16 && $bmi <= 18.5) {
                $output = "Underweight";
            } elseif ($bmi > 18.5 && $bmi <= 25) {
                $output = "Normal";
            } elseif ($bmi > 25 && $bmi <= 30) {
                $output = "Overweight";
            } elseif ($bmi > 30 && $bmi <= 35) {
                $output = "Obesity class 1";
            } elseif ($bmi > 35 && $bmi <= 40) { // Fixed typo: $num → $bmi
                $output = "Obesity class 2";
            } else { // Catch all values over 40
                $output = "Obesity class 3";
            }

            // Display result
            echo "<p>Your BMI is " . $bmi . " and you are : " . $output . "</p>";
        } else {
            // Show error for invalid inputs
            echo "<p style='color: red;'>Please enter valid, non-zero numeric values for height and weight!</p>";
        }
    }
    ?>
</div>

Key Fixes Made

  1. Form Setup: Added method="post" to the form and name="submit" to the submit button, so we can reliably detect form submission using CodeIgniter's built-in input methods.
  2. Conditional Execution: Wrapped all BMI calculation logic inside a submission check—this prevents the code from running on initial page load, eliminating undefined variable errors.
  3. Input Validation: Added checks to ensure inputs are non-empty, numeric, and height is greater than 0—stops division by zero and invalid value issues.
  4. Bug Squashing: Fixed the typo $num to $bmi in one conditional, and adjusted the last category to properly catch all values over 40.
  5. User Experience: Added input value retention (so users don't lose their entries after submission) and rounded BMI for readability, plus an error message for invalid inputs.

内容的提问来源于stack exchange,提问作者user9776860

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:22:11