Okay, I asked a question earlier but I’ve run into a whole new problem. I have a PHP array in a PHP script server side. I am trying to write a Client side Ajax script to repoll the PHP script for new data and update the statistics displayed on the page.
Heres the Ajax that im sure im not doing right:
setInterval(function(getLatestInfo)
{
$.ajax({
type: 'POST',
url: 'script.php',
data: 'id=data',
dataType: 'json',
cache: false,
success: function(getLatestInfo){
$.getJSON(script.php, function(getLatestInfo){
var result_array = JSON.parse(result);
});
$('#bit_rate').html(result_array[4]);
$('#listeners').html(result_array[5]);
$('#current_song').html(result_array[9]);
});
});
}, 10000);//time in milliseconds
And here is the PHP:
<?php
Function getLatestInfo() {
$SERVER = 'http://chillstep.info:1984';
$STATS_FILE = '/status.xsl?mount=/test.mp3';
$LASTFM_API= '00000000000000000';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$SERVER.$STATS_FILE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
curl_close($ch);
$dp = array();
$search_for = "<td\s[^>]*class=\"streamdata\">(.*)<\/td>";
$search_td = array('<td class="streamdata">','</td>');
if(preg_match_all("/$search_for/siU",$output,$matches)) {
foreach($matches[0] as $match) {
$to_push = str_replace($search_td,'',$match);
$to_push = trim($to_push);
array_push($dp,$to_push);
}
}
$x = explode(" - ",$dp[9]);
echo json_encode($dp);
}
?>
In short, I need this ajax script to update and pull the PHP variable $dp, which is an array, parse it out, and create HTML usable string variables. And repeat the process every 10 seconds.
Don’t use setInterval in this instance. In the success callback call the original function again. Also you are calling getJSON in the success callback of the ajax call – this is duplicating calls.
For example
function getlatest() { $.ajax({ //rest of ajax param success : function(results){ var resultarray = $.parseJSON(results); $('#bit_rate').html(result_array[4]); $('#listeners').html(result_array[5]); $('#current_song').html(result_array[9]); getlatest(); } }); }