123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <!DOCTYPE html>
- <!--
- Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- -->
- <html lang="es">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
- <meta name="author" content="Echedey López Romero" />
- <title>Calculadora de IMC</title>
- <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
- <script src="./jquery/jquery-3.5.1.slim.min.js"></script>
- <script src="./bootstrap/js/bootstrap.bundle.min.js"></script>
- <style>
- .centro {
- text-align: center;
- }
- </style>
- <script>
- function Limpiar() {
- let Operadores = document.getElementsByTagName("input");
- for (let Pos = 0; Pos < Operadores.length; Pos++) {
- Operadores[Pos].setAttribute("value", "");
- }
- let IMCSpan = document.getElementById("imcspan");
- let IMC = document.getElementById("imc");
- let DescripcionSpan = document.getElementById("descripcionspan");
- let Descripcion = document.getElementById("descripcion");
- IMCSpan.textContent = "IMC";
- IMC.textContent = "--";
- DescripcionSpan.textContent = "Descripción";
- Descripcion.textContent = "--";
- }
- </script>
- </head>
- <body class="container">
- <?php
- // Mostrar errores por pantalla
- ini_set('display_errors', '1');
- ini_set('display_startup_errors', '1');
- error_reporting(E_ALL);
- // Inicializar variables iniciales
- $IMC = "";
- $Descripcion = "";
- $Peso = "";
- $Altura = "";
- // Comprobar que se realizar operacion
- if (isset($_REQUEST["operacion"])) {
- $Operacion = $_REQUEST["operacion"];
- // Validar el tipo de operacion
- if ($Operacion == "calcular") {
- // Comprobar que se envian los operadores
- if (!isset($_REQUEST["peso"]) || !isset($_REQUEST["altura"])) {
- $IMC = "ERROR";
- $Descripcion = "Deben enviarse los 2 operadores.";
- } else {
- // Establecer valor a los operadores
- $Peso = $_REQUEST["peso"];
- $Altura = $_REQUEST["altura"];
- // Validar el tipo de valor de los operadores
- if ($Peso == "" || $Altura == "") {
- $IMC = "ERROR";
- $Descripcion = "Los operadores deben tener valor.";
- } else if (is_numeric($Peso) && is_numeric($Altura)) {
- if ($Peso == 0 || $Altura == 0) {
- $IMC = "ERROR";
- $Descripcion = "Los operadores deben tener valor distinto de 0.";
- } else if ($Peso < 0 || $Altura < 0) {
- $IMC = "ERROR";
- $Descripcion = "Los operadores deben tener valor positivo.";
- } else {
- $IMC = round($Peso / ($Altura * $Altura), 2);
- // Comprobar descripcion correspondiente
- if ($IMC < 18.5) {
- $Descripcion = "<span style=\"color: red\">Delgadez</span>";
- } else if ($IMC >= 18.5 && $IMC < 25) {
- $Descripcion = "<span style=\"color: green\">Peso normal</span>";
- } else if ($IMC >= 25 && $IMC < 29.9) {
- $Descripcion = "<span style=\"color: orange\">Sobrepeso</span>";
- } else {
- $Descripcion = "<span style=\"color: red\">Obesidad</span>";
- }
- }
- } else {
- $IMC = "ERROR";
- $Descripcion = "Los operadores deben tener valor numérico.";
- }
- }
- } else {
- $IMC = "ERROR";
- $Descripcion = "La operación debe ser «calcular».";
- }
- }
- ?>
- <div class="row my-3">
- <div class="col-lg-3 col-md-2 col-sm-1"></div>
- <div class="col-lg-6 col-md-8 col-sm-10 col-xs-12 border border-primary rounded p-3">
- <h1 class="centro">Calculadora de IMC</h1>
- <form action="./index.php" method="POST" class="mb-2">
- <div class="form-group">
- <label for="peso">Peso</label>
- <input type="number" name="peso" id="peso" value="<?php echo($Peso) ?>"
- placeholder="0" step="0.1" required class="form-control form-control-lg" />
- </div>
- <div class="form-group">
- <label for="altura">Altura</label>
- <input type="number" name="altura" id="altura" value="<?php echo($Altura) ?>"
- placeholder="0" step="0.01" required class="form-control form-control-lg" />
- </div>
- <?php
- if ($IMC == "ERROR") {
- ?>
- <span id="imcspan" class="d-block mb-2 centro">¡Aviso!</span>
- <p id="imc" class="border border-secondary rounded mb-2 p-2 centro"><?php echo($IMC) ?></p>
-
- <span id="descripcionspan" class="d-block mb-2 centro">Interpretación</span>
- <p id="descripcion" class="border border-secondary rounded p-2 centro"><?php echo($Descripcion) ?></p>
- <?php
- } else if (is_numeric($IMC)) {
- ?>
- <span id="imcspan" class="d-block mb-2 centro">IMC</span>
- <p id="imc" class="border border-secondary rounded mb-2 p-2 centro"><?php echo($IMC) ?></p>
-
- <span id="descripcionspan" class="d-block mb-2 centro">Descripción</span>
- <p id="descripcion" class="border border-secondary rounded p-2 centro"><?php echo($Descripcion) ?></p>
- <?php
- } else {
- ?>
- <span id="imcspan" class="d-block mb-2 centro">IMC</span>
- <p id="imc" class="border border-secondary rounded mb-2 p-2 centro">--</p>
- <span id="descripcionspan" class="d-block mb-2 centro">Descripción</span>
- <p id="descripcion" class="border border-secondary rounded p-2 centro">--</p>
- <?php } ?>
- <button type="submit" name="operacion" value="calcular"
- class="btn btn-primary btn-lg btn-block mb-1">Calcular</button>
- <button type="reset" class="btn btn-danger btn-lg btn-block"
- onclick="Limpiar()">Limpiar</button>
- </form>
- </div>
- <div class="col-lg-3 col-md-2 col-sm-1"></div>
- </div>
- </body>
- </html>
|