Update a div ajax

1

I want to get to do a div recharge only when it finds a modification in the database I currently have this so, if you can help me I would appreciate it is for a shift system that I am doing:

 <?php


require_once("../../db/db.php"); 

$result = $mysqli->query("SELECT * FROM  'turnos' ORDER by 'turno' desc limit 1" ); 
$row = $result->fetch_row(); 

      echo '<script type="text/javascript">
      swal({ title: "siguiente turno : '.$row[0].'", 
      text: "Se envio correctamente", 
      type: "success",
      showConfirmButton: false,
      timer: 2000
    });
     </script>'; 

?> 

this is the ajax:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>


<script>
setInterval(function() {
  $('#turnos').load('./ajax/turnos.php');// Selector de la div y el fichero a refrescar
}, 1000); // Temporizador que ejecuta el refresco cada 1 segundos
</script>

div:

<div id="turnos"></div>
    
asked by steven 02.05.2018 в 18:40
source

1 answer

0
<?php

    $result = $mysqli->query("SELECT * FROM 'turnos' ORDER by 'turno' desc limit 1" );



    $row = $result->fetch_row(); 

    echo json_encode(['turno'=>$row[0]]);

    var turno_actual=0;

    function desplegarAlerta() {
   // despliego la alerta desde el front
   swal({
    title: "siguiente turno : " + turno_actual,
    text: "Se envio correctamente",
    icon: "success",
    button: true
  });
}

function refrescaTurno() {



      $.ajax({
     url: "./ajax/turnos.php",
     dataType:'json' // parseo la respuesta como objeto
   }).then(function(response) {
      // sólo lo ejecutas si el turno cambió
      if(response.turno != turno_actual) {
         turno_actual=response.turno;
         desplegarAlerta();
      }
      window.setTimeout(refrescaTurno,2000);
   });

}

refrescaTurno();
    
answered by 08.08.2018 / 23:27
source