0

I need to update my UI (webpage) periodically with new values. I have PHP code that grabs the values form a database, but I am not 100% sure on what is the best way of getting that data to the UI side.

The complication here is that: 1. I need to constantly update the values (every second) 2. I need to update many different areas of the page independently

Thanks,

hobodave
  • 28,925
  • 4
  • 72
  • 77
Goro
  • 9,919
  • 22
  • 74
  • 108

3 Answers3

5

You use javascript to ask a PHP script for the values. You don't push from PHP to javascript.

You'll probably want to use AJAX and setInterval()

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
3

What you probably want is comet, not setInterval as you'll overwhelm your servers with a lot of unnecessary traffic.

This is what Facebook does for their chat features, and their live feeds (or at least used to do, I haven't looked at it in quite some time.

cgp
  • 41,026
  • 12
  • 101
  • 131
  • Yes. For now you'll have to look in to doing comet. If your planning on getting a product out soon I wouldn't bother with WebSockets just yet, only Webkit supports them so far! :) – Ronald Feb 24 '10 at 02:18
2

You should also look into http://dev.w3.org/html5/websockets/ More and more browsers starts to support them and it's a good way to constantly pass information back and forth.

You can check for browser support and use it if it exist.

Milar
  • 156
  • 5