index.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <!DOCTYPE html>
  2. <!--
  3. Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. -->
  15. <html lang="es">
  16. <head>
  17. <meta charset="UTF-8" />
  18. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  19. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  20. <meta name="author" content="Echedey López Romero" />
  21. <title>Calculadora de IMC</title>
  22. <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
  23. <script src="./jquery/jquery-3.5.1.slim.min.js"></script>
  24. <script src="./bootstrap/js/bootstrap.bundle.min.js"></script>
  25. <style>
  26. .centro {
  27. text-align: center;
  28. }
  29. </style>
  30. <script>
  31. function Limpiar() {
  32. let Operadores = document.getElementsByTagName("input");
  33. for (let Pos = 0; Pos < Operadores.length; Pos++) {
  34. Operadores[Pos].setAttribute("value", "");
  35. }
  36. let IMCSpan = document.getElementById("imcspan");
  37. let IMC = document.getElementById("imc");
  38. let DescripcionSpan = document.getElementById("descripcionspan");
  39. let Descripcion = document.getElementById("descripcion");
  40. IMCSpan.textContent = "IMC";
  41. IMC.textContent = "--";
  42. DescripcionSpan.textContent = "Descripción";
  43. Descripcion.textContent = "--";
  44. }
  45. </script>
  46. </head>
  47. <body class="container">
  48. <?php
  49. // Mostrar errores por pantalla
  50. ini_set('display_errors', '1');
  51. ini_set('display_startup_errors', '1');
  52. error_reporting(E_ALL);
  53. // Inicializar variables iniciales
  54. $IMC = "";
  55. $Descripcion = "";
  56. $Peso = "";
  57. $Altura = "";
  58. // Comprobar que se realizar operacion
  59. if (isset($_REQUEST["operacion"])) {
  60. $Operacion = $_REQUEST["operacion"];
  61. // Validar el tipo de operacion
  62. if ($Operacion == "calcular") {
  63. // Comprobar que se envian los operadores
  64. if (!isset($_REQUEST["peso"]) || !isset($_REQUEST["altura"])) {
  65. $IMC = "ERROR";
  66. $Descripcion = "Deben enviarse los 2 operadores.";
  67. } else {
  68. // Establecer valor a los operadores
  69. $Peso = $_REQUEST["peso"];
  70. $Altura = $_REQUEST["altura"];
  71. // Validar el tipo de valor de los operadores
  72. if ($Peso == "" || $Altura == "") {
  73. $IMC = "ERROR";
  74. $Descripcion = "Los operadores deben tener valor.";
  75. } else if (is_numeric($Peso) && is_numeric($Altura)) {
  76. if ($Peso == 0 || $Altura == 0) {
  77. $IMC = "ERROR";
  78. $Descripcion = "Los operadores deben tener valor distinto de 0.";
  79. } else if ($Peso < 0 || $Altura < 0) {
  80. $IMC = "ERROR";
  81. $Descripcion = "Los operadores deben tener valor positivo.";
  82. } else {
  83. $IMC = round($Peso / ($Altura * $Altura), 2);
  84. // Comprobar descripcion correspondiente
  85. if ($IMC < 18.5) {
  86. $Descripcion = "<span style=\"color: red\">Delgadez</span>";
  87. } else if ($IMC >= 18.5 && $IMC < 25) {
  88. $Descripcion = "<span style=\"color: green\">Peso normal</span>";
  89. } else if ($IMC >= 25 && $IMC < 29.9) {
  90. $Descripcion = "<span style=\"color: orange\">Sobrepeso</span>";
  91. } else {
  92. $Descripcion = "<span style=\"color: red\">Obesidad</span>";
  93. }
  94. }
  95. } else {
  96. $IMC = "ERROR";
  97. $Descripcion = "Los operadores deben tener valor numérico.";
  98. }
  99. }
  100. } else {
  101. $IMC = "ERROR";
  102. $Descripcion = "La operación debe ser «calcular».";
  103. }
  104. }
  105. ?>
  106. <div class="row my-3">
  107. <div class="col-lg-3 col-md-2 col-sm-1"></div>
  108. <div class="col-lg-6 col-md-8 col-sm-10 col-xs-12 border border-primary rounded p-3">
  109. <h1 class="centro">Calculadora de IMC</h1>
  110. <form action="./index.php" method="POST" class="mb-2">
  111. <div class="form-group">
  112. <label for="peso">Peso</label>
  113. <input type="number" name="peso" id="peso" value="<?php echo($Peso) ?>"
  114. placeholder="0" step="0.1" required class="form-control form-control-lg" />
  115. </div>
  116. <div class="form-group">
  117. <label for="altura">Altura</label>
  118. <input type="number" name="altura" id="altura" value="<?php echo($Altura) ?>"
  119. placeholder="0" step="0.01" required class="form-control form-control-lg" />
  120. </div>
  121. <?php
  122. if ($IMC == "ERROR") {
  123. ?>
  124. <span id="imcspan" class="d-block mb-2 centro">¡Aviso!</span>
  125. <p id="imc" class="border border-secondary rounded mb-2 p-2 centro"><?php echo($IMC) ?></p>
  126. <span id="descripcionspan" class="d-block mb-2 centro">Interpretación</span>
  127. <p id="descripcion" class="border border-secondary rounded p-2 centro"><?php echo($Descripcion) ?></p>
  128. <?php
  129. } else if (is_numeric($IMC)) {
  130. ?>
  131. <span id="imcspan" class="d-block mb-2 centro">IMC</span>
  132. <p id="imc" class="border border-secondary rounded mb-2 p-2 centro"><?php echo($IMC) ?></p>
  133. <span id="descripcionspan" class="d-block mb-2 centro">Descripción</span>
  134. <p id="descripcion" class="border border-secondary rounded p-2 centro"><?php echo($Descripcion) ?></p>
  135. <?php
  136. } else {
  137. ?>
  138. <span id="imcspan" class="d-block mb-2 centro">IMC</span>
  139. <p id="imc" class="border border-secondary rounded mb-2 p-2 centro">--</p>
  140. <span id="descripcionspan" class="d-block mb-2 centro">Descripción</span>
  141. <p id="descripcion" class="border border-secondary rounded p-2 centro">--</p>
  142. <?php } ?>
  143. <button type="submit" name="operacion" value="calcular"
  144. class="btn btn-primary btn-lg btn-block mb-1">Calcular</button>
  145. <button type="reset" class="btn btn-danger btn-lg btn-block"
  146. onclick="Limpiar()">Limpiar</button>
  147. </form>
  148. </div>
  149. <div class="col-lg-3 col-md-2 col-sm-1"></div>
  150. </div>
  151. </body>
  152. </html>