Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to refresh multiple PHP values every x seconds?
    text
    copied!<p>I have an <code>index.php</code> file that I would like to run <code>getdata.php</code> every 5 seconds.</p> <p><code>getdata.php</code> returns multiple variables that need to be displayed in various places in <code>index.php</code>.</p> <p>I've been trying to use the jQuery <code>.load()</code> function with no luck.<br> It's refreshing the 12 <code>&lt;div&gt;</code> elements in various places on the <code>index.php</code>, but it's not re-running the <code>getdata.php</code> file that should get the newest data.</p> <p>But If I hit the browser refresh button, the data is refreshed.</p> <p><code>getdata.php</code> returns about 15 variables.</p> <p>Here is some sample code:</p> <pre><code>&lt;script&gt; var refreshId = setInterval(function() { $('#Hidden_Data').load('GetData.php'); // Shouldn´t this return $variables $('#Show_Data_001').fadeOut("slow").fadeIn("slow"); $('#Show_Data_002').fadeOut("slow").fadeIn("slow"); $('#Show_Data_003').fadeOut("slow").fadeIn("slow"); $('#...').fadeOut("slow").fadeIn("slow"); }, 5000); // Data refreshed every 5 seconds */ &lt;/script&gt; </code></pre> <p>Here's an example of <code>GetData.php</code>:</p> <pre><code>$query = "SELECT column1, COUNT(column2) AS variable FROM table GROUP BY column"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ $column1 = $row['column1 ']; $variable = $row['variable ']; if($column1 == "Text1") { $variable1 = $variable; } elseif($column1 == "Text2") { $variable2 = $variable; } ... continues to variable 15 ... } </code></pre> <p>Then further down the page the HTML elements display the data:</p> <pre><code>&lt;div id="Hidden_Data"&gt;&lt;/div&gt; &lt;div id="Show_Data_001"&gt;&lt;?php echo $variable1; ?&gt;&lt;/div&gt; &lt;div id="Show_Data_002"&gt;&lt;?php echo $variable2; ?&gt;&lt;/div&gt; &lt;div id="Show_Data_003"&gt;&lt;?php echo $variable3; ?&gt;&lt;/div&gt; ... </code></pre> <p>I tried using the data parameter as suggested here: <a href="https://stackoverflow.com/a/8480059/498596">https://stackoverflow.com/a/8480059/498596</a></p> <p>But I couldn't fully understand how to load all the variables every 5 seconds and call them on the index page.</p> <p>Today the <code>GetData.php</code> page just returns <code>$variable1 = X; $variable2 = Y</code> and so on.</p> <p><strong>UPDATE</strong><br> For some reason the jQuery is not loading the <code>GatData.php</code> file and refreshing the variables.</p> <p>I tried adding to "Hidden_Data" to the <code>include('GetData.php')</code> and then the variables are readable on the page.</p> <p>If I remove this part, the page displays "variable not set" warning that suggesting that the jQuery is not loading the <code>GetData.php</code> script into the <code>Hidden_Data</code> <code>&lt;div&gt;</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload