1

So I have a page that uses some JQuery to reload a php file every 15 seconds, nothing special. The php file simply echos that last 3 lines of a textfile.

Here's the JQuery:

$(document).ready(function() {
    $("#realtime").load("realtime.php");

    var refreshId = setInterval(function() {
        $("#realtime").load('realtime.php');
    }, 15000);
    $.ajaxSetup({ cache: false });
});

And lets just say for a clear example the PHP file is:

echo "<li class='alternate'>query 1</li>";
echo "<li class='alternate'>query 2</li>";
echo "<li class='alternate'>query 3</li>";

I would like to achieve 2 things, firstly I would only like the PHP file to update when a form has been submitted in real time so it's not just updating every 15 seconds, I am guessing this will involve some kind of cache storage or something?

Secondly you may have noticed the li class is named alternate because the colors alternate how would I be able to change this from odd to even when the form has recently been submitted also. Jquery so far is:

$(document).ready(function(){
    $('#myList li:nth-child(odd)').addClass('alternate');
});

I have most of the pieces but placing them together is puzzling me although it may seem very simple, can anyone help? Thanks.

  • "Firstly I would only like the PHP file to update when a form has been submitted in real time so it's not just updating every 15 seconds" I'm not quite clear what you mean here – can you explain in a bit more detail? Is your 15s loop just there for testing, or do you require that in the finished version? – Rich Bradshaw Nov 11 '12 at 13:10
  • Sounds like a use case for Comet, but PHP is a horrible environment for that: http://stackoverflow.com/questions/603201/using-comet-with-php – jsalvata Nov 11 '12 at 13:15
  • I require something like that in the finished version, the idea is that when a form is recently submitted that form will update a text file containing recently submitted queries. Now, on the page it is constantly reloading that PHP that echo the recent queries from the text file. The idea is to only reload "realtime.php" if the form has recently been submitted. That way way the server is not constantly refreshing every 15 seconds. By doing this I would then like to alternate the li from odd to even so the li background colors re-arrange real time as well, of course when the form submits. –  Nov 11 '12 at 13:17
  • Checking if something is 'fresh' will take a ajax call as well. So unless your realtime.php is processor intensive, I woulnd't do that. Just call that page every 15s. No matter if there is new data or not. – Gerben Jacobs Nov 11 '12 at 13:54
  • Yeah, I guess I just wanted to alternate the li from odd to even when there are new queries so it looks like the last query has moved down the list, because the color switches blue, grey, blue grey etc. If I automate odd to even on every refresh and there is no new queries it will just look like the existing queries (in the li) are switching around. –  Nov 11 '12 at 14:02
  • What is your form's action set to? Do you have a submit handler? Is this entirely AJAX-driven? – pete Nov 11 '12 at 16:04
  • It is a POST form. It writes to files like queries.txt. For example, realtime.php will read from queries.txt. The only AJAX involved is on the form page of which is the 15 second refresh. –  Nov 11 '12 at 19:47

0 Answers0