Update a PHP variable inside Javascript in a time interval

Asked

Viewed 281 times

-3

We have a postgresql query within a javascript function which is as follows:

function  valor(){  
    <?php 
        $con_string = "host='ip' port=numero dbname='banco' user='usuario' password='***'";
        $conn = pg_connect($con_string);
        $query="SELECT tubeteira from velocidadereal";
        $output=pg_query($conn,$query);
        $retorna =  pg_fetch_array($output);
        $valor = $retorna["tubeteira"];
    ?>

    return <?php echo $valor ?>;
} 

setInterval(valor,1000);

But although it updates from 1 to a second the php variable is not updating in the function, so by changing the value in the database of the column where we are performing the select it does not return the new value unless we have refreshed the page.

How can I automatically update?

Important to note that the consultation is feeding a graph.

  • 2

    This PHP query is executed only once, by the server, only on the page load, regardless of whether you call the value() function several times, because it is on the client side. To update the variable you will need to do the query in PHP via ajax. I believe someone will give you an answer with an example. I made the comment just for an initial understanding.

  • Thanks for the comment. I am waiting for an example.

  • 1

    Renata, you put "We have a postgresql query inside an AJAX function which is as follows:" But there is no AJAX function there. I recommend this article to you: https://www.devmedia.com.br/executando-consultas-ao-mysql-com-php-e-ajax/26008

1 answer

4

PHP is a language server-side (that works next to the server). Javascript is a language client-side (that works on the client side [read "browser"]).

You cannot run a Javascript function waiting for it to affect the behavior of a PHP variable. That’s how to wait for a "magic to happen".

The solution you can apply in such cases are numerous, but here I will quote one of them:

  • Make AJAX request of X in X periods.
  • Using a Websocket

An example of how to do with AJAX:

  1. Create a PHP script to return the data in JSON.

Code:

$con_string = "host='ip' port=numero dbname='banco' user='usuario' 
password='***'";
$conn = pg_connect($con_string);
$query="SELECT tubeteira from velocidadereal";
$output=pg_query($conn,$query);
$retorna =  pg_fetch_array($output);
$valor = $retorna["tubeteira"];


header('Content-Type: application/json');

exit(json_encode(['valor' => $valor]));

Create a function in Javascript that requests through AJAX:

 function valor(valor) {
     // faça alguma coisa como  valor obtido de 1 em 1 segundo
 }

setInterval(function atualizarValor() {

    if (atualizarValor.executando) return;

    atualizarValor.executando = true;

     $.ajax({
         url: '/script_json.php',
         success: function (response) {

            valor(response.valor);

            atualizarValor.executando = false;
         }
     }) 
}, 1000);

Observing: Beware of the amount of requests made on your server, as this may impair your server’s performance, depending on the number of X users simultaneous requests.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.