0

I think I've been going about using PHP the wrong way. From what I've recently come to understand, all PHP code is "rendered" when the page is loaded, but just that once and that's it. So how would I go about doing something like this?

window.setInterval(tick,3000);

function tick(){
    document.getElementById("time").innerText=" <?php echo date('s'); ?> ";
}

This just constantly updates the "time" div to whatever time it was when the webpage first loaded, which is obviously not what I'm trying to do.

This is just an example; what I'm looking to do eventually is update a webpage with information from an ODBC connection via PHP without having to reload the webpage every few seconds. I'm new to PHP programming, and I keep seeing stuff about transactions and requests, but all of the examples I see are a little over my head, regarding forms and server databases. I'm not using a form, there's really no user interaction of any sort; how would I make something like this work?

I've got a version that works perfectly as long as I'm okay with refreshing the page every second or two, but if I leave it running for more than a couple hours the browser eats up all the memory and crashes, so I'm looking for alternatives that don't involve refreshing.

Also, since I know it's going to come up, the machine I'm putting this on doesn't have any sort of internet connection, and I'm trying to avoid installing any extra drivers or files, which is why I've been avoiding AJAX.

NickGlowsinDark
  • 337
  • 1
  • 6
  • 17
  • 4
    If your 'machine' can execute PHP and run javascript -- you have all you need to use AJAX without any extra drivers or files. – phpisuber01 May 15 '14 at 20:42
  • Why insist on shoehorning in PHP or AJAX into your JS for things easily accomplished within JS itself? http://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript – Sammitch May 15 '14 at 20:49
  • Have you tried using Javascript's `Date` object instead of PHP's? – Kyle May 15 '14 at 20:49
  • 3
    date is just an example, if you read the whole question ... –  May 15 '14 at 20:50
  • I looked at the XMLHttpRequest() method early on, but disregarded it almost immediately after seeing the ActiveX objects. I should have mentioned before that I'm using a Raspberry Pi as both the webserver and page display-er, just as a way to pull information off of a database stored on a separate computer. – NickGlowsinDark May 15 '14 at 20:51
  • @NickGlowsinDark — Browsers have had non-ActiveX implementations of XMLHttpRequest for about a decade. – Quentin May 15 '14 at 20:55
  • I think the plain JavaScript approaches to AJAX appear to be using ActiveX, but this is just the Internet Explorer approach, for which a specific bit of code is (or was) required. It doesn't actually invoke an ActiveX control. As per [the accepted answer](http://stackoverflow.com/a/7165616/472495) on the first duplicate, you can use a wrapper library like jQuery. – halfer May 15 '14 at 20:55
  • Unless you are supporting IE7, there's no need to worry about the ActiveX fallback. – Quentin May 15 '14 at 20:56

0 Answers0