I need to keep the data on screen when refreshing

0

, Good evening, I'm in the process of putting together a hotel website for the school, but I have a problem, the page when I start, the rooms appear in green, when I press a room it turns red and sends the data to the BD. But when refreshing the page, it turns green again, I would like to know how I do so that the refreshing page shows me the data of the BD.

This is the code of the main thing, the other is the connection and the UPDATE in the DB.

Thank you very much already.

<table width="50%" class="medio" border="black" align="center">
          <tr>
            <?php 
              session_start();
              include('conexion.php');
              $sql= "SELECT * FROM habitaciones";
              $resultado= mysqli_query($con, $sql);
             ?>
            <td class="hab" data-estado='0' data-nro="HAB1" align="center"><p>Habitación 1</td>
            <td class="hab" data-estado='0' data-nro="HAB2" align="center"><p>Habitación 2</td>
            <td class="hab" data-estado='0' data-nro="HAB3" align="center"><p>Habitación 3</td>
            <td class="hab" data-estado='0' data-nro="HAB4" align="center"><p>Habitación 4</td>
          </tr>
          <tr>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
          </tr>
          <tr>
        </table>
        <br>
        <table width="50%" border="black" align="center">
          <tr>
            <td class="hab" data-estado='0' data-nro="HAB5" align="center"><p>Habitación 5</td>
            <td class="hab" data-estado='0' data-nro="HAB6"  align="center"><p>Habitación 6</td>
            <td class="hab" data-estado='0' data-nro="HAB7" align="center"><p>Habitación 7</td>
            <td class="hab" data-estado='0' data-nro="HAB8" align="center"><p>Habitación 8</td>
          </tr>
          <tr>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
            <td align="center"><p>Facturación</p></td>
          </tr>
      </table>

      <script src="jquery.min.js"></script>
        <script>
        function change(elemento) {
          var estado = $(elemento).data('estado')
          if(estado == "0"){
            $(elemento).css({'background-color': "#008000"});
          }else{
            $(elemento).css({'background-color': "#B40404"});
          }
        }     
      function actualizar_habitacion(element){
        var that = $(element);
        var nro = $(element).data('nro');   
        var estado = $(element).data('estado');
            if (estado == 1){
            estado = 0;
        }else{
          estado= 1;
        }
        var data = {'nro_habitacion': nro, 'estado': estado};
        console.log(data)
        $.ajax({
          url: "habitacion.php",
          data: data,
          type:'POST',
          //dataType: "json",
        }).done(function() {
          $(that).data('estado', estado)
          change($(that));
        })
        .fail(function( jqXHR, textStatus, errorThrown ) {

             console.log( "FALLO!!!: " +  textStatus + "\ndetalle: " +errorThrown);

        });        
      }   
      $.each($(".hab"), function(index, value){
        change($(value));
        $(value).on('click', function(){
          actualizar_habitacion($(this));
        });
      });
      </script>  

habitación.php

<?php 
session_start(); 
include('conexion.php');


$estado=$_POST['estado'];
$nro_habitacion=$_POST['nro_habitacion'];


    //if( isset($_POST['nro_habitacion']) && isset($_POST['estado']) ) {
        $sql = "UPDATE habitaciones SET estado='$estado' WHERE nro_habitacion= '$nro_habitacion'";

        $resultado = mysqli_query($con, $sql);

//}
?>

conexion.php

<?php

$servername = "localhost";
$username = "root";
$password = "";
$db = "hoteleria";

// Create connection
$con = mysqli_connect($servername, $username, $password,$db);

// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

?>
    
asked by Elizabeth 21.11.2018 в 05:35
source

0 answers