Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!