Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda