Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

94
Vistas
Ask db information every tot seconds

I'm trying to refresh data on my web page according to the data stored in the database, so every 2 seconds I call with an ajax request a php file. The called php script is this:

session_start();

.....Connection to the db.......

$prova = pg_query($connect, "SELECT * FROM maxdistance");

$prova2 = "";
while ($row = pg_fetch_row($prova)) {
     $prova2 = $prova2.$row[0].$row[1].$row[2];
}

$_SESSION['prova'] = $prova2;

And this is the code in javascript:

var intervalId = window.setInterval(function(){
  newPositions();
}, 2000);

function newPositions(){
    $(document).ready(function(){
                $.ajax({
                      type: "POST",
                      url: "realTimePosition.php",
                      success: function(msg){
                          provaaa = <?php echo ($_SESSION[prova]); ?>;
                      }
                })
   });

The problem is that, when I refresh the page the code run and in the variable provaaa is stored the value 20 (the actual value in the db) but if I modify the value in the db, the value of the variable is the same, why is this happening?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you cant mix JS and PHP like this

problem part

 provaaa = <?php echo ($_SESSION[prova]); ?>;

solution

  • your PHP script have to send back REPLY with "echo"
echo json_encode([ 'data' => $prova2]);
  • then y have to catch server response inside your "success" callback dunction where param is RESPONSE from server so
success: function(res){
                          provaaa = JSON.parse(res).data;
                      }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda