PHP - ¿Como corregir el error de alerta Undefined?

 
Vista:
Imágen de perfil de Angel
Val: 27
Ha aumentado su posición en 24 puestos en PHP (en relación al último mes)
Gráfica de PHP

¿Como corregir el error de alerta Undefined?

Publicado por Angel (14 intervenciones) el 15/04/2021 22:08:02
Sigo avanzando con mi práctica de proyecto de hotel y sus 15 habitaciones, pero surgen detalles en esta ocasión, me arroja un alerta undefined algo indefinido.

Al momento de dar clic en cualquier botón no llega actualizar la base de datos.

Ya que JavaScript me esta arrojando ese problema por medio del alert.


Captura


Update.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
include ("conexion.php");
 
$idHabitacion = (int)$_POST['idHabitacion']??0;
$estatus = $_POST['estatus']??'';
 
if ($idHabitacion > 0 && ($estatus == 'Ocupado' || $estatus == 'Disponible')) {
    $consulta = "UPDATE habitaciones SET estatus = '$estatus' WHERE idHabitacion = '$idHabitacion'";
 
    if ($conexion->query($consulta)) {
        echo 'La habitación $idHabitacion ahora tiene estado $estatus';
    } else {
        echo 'Hubo un error al actualizar habitación';
    }
} else {
    echo 'No se recibieron correctamente ID de habitación y estatus';
}
?>

Código Javascript :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function estatusHabitacion(e) {
  let btn = e.target;
  btn.classList.toggle("rentado");
  let rentado = btn.classList.contains("rentado");
 
  let habitacion = btn.textContent.replace("H", "");
  console.log(habitacion, rentado);
 
  habitacion = {
    idHabitacion: habitacion,
    estatus: rentado ? "Ocupado" : "Disponible",
  };
  let options = {
    method: "POST",
    headers: {
      "Content-Type": "application/json;charset=utf-8",
    },
    body: JSON.stringify(habitacion),
  };
 
  fetch("Update.hphp", options)
    .then((resp) => {
      if (resp.ok) {
        return resp.text();
      }
      console.log("Error de petición de Ajax con el servidor");
    })
 
    .then((text) => {
      alert(text);
    })
    .catch((e) => {
      console.log("Hubo un problema con la petición Fetch:" + e.message);
    });
}
 
document.addEventListener("DOMContentLoaded", () => {
  let habitacion = document.querySelectorAll(".habitacion");
  habitacion.forEach((habitacion) => {
    habitacion.addEventListener("click", estatusHabitacion);
  });
});
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de joel
Val: 3.828
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

¿Como corregir el error de alerta Undefined?

Publicado por joel (1269 intervenciones) el 16/04/2021 11:20:57
Hola Angel, segun veo, es porque no recibe el ok en la respuesta al fetch, y creo que es porque en vez de poner:
1
Update.php
has puesto:
1
Update.hphp
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar