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
nameattribute, 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
$weightand$heightvariables that haven't been defined yet. - Division by zero: Same as above—on initial page load,
$heightis undefined, so$height * $heightevaluates 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
- Form Setup: Added
method="post"to the form andname="submit"to the submit button, so we can reliably detect form submission using CodeIgniter's built-in input methods. - 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.
- 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.
- Bug Squashing: Fixed the typo
$numto$bmiin one conditional, and adjusted the last category to properly catch all values over 40. - 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
相关产品推荐
相关产品推荐

