0

I want to change the content of a div when I click a button. I can do this easily with jquery but it is only on my side. I want it to be changed for all the users if they connect to the page or refresh the page. How can I do that? I understand that it has to be with php (server side script). Any ideas? To make it simple -> click of a button -> change div for all visitors of that page (after they refresh the page of course).

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110

4 Answers4

2

One question here is how do you make inter-session communication happen. Details aside, there are two ways to do it: (1) via client-side polling, or (2) via server-side broadcasting.

Polling is easy, but not ideal. Clients (javascript-ajax) check the server (every second or few seconds) continuously for updates, and when one happens, they apply whatever update/message. Problem is, if this is critical, that clients will be out of sync. At times, they might drift far away from each other, and the server won't know it.

In terms of server-side options, you can use cometd (http://cometd.org/). It basically gives you an ability to synchronize web browser actions and message individual sessions. This tutorial, mentioned in other SO questions, will get you a quick start on PHP-cometd and ajax: http://www.zeitoun.net/articles/comet_and_php/start

Edit: was checking on this protocol usage, and apparently gmail and facebook both use it or minor variants of it. See How does facebook, gmail send the real time notification? and How does GMail implement Comet? for some interesting reads.

Community
  • 1
  • 1
pp19dd
  • 3,625
  • 2
  • 16
  • 21
1

Use ajax to post to a php page, which updates a DB, which is the source of the content...

JKirchartz
  • 17,612
  • 7
  • 60
  • 88
1

you would have to do an ajax function, that returns the changed content of the page, and then reload the page.

Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
1

what I would do is:

  1. when button clikced: store the new value to the database.
  2. set a timeout to refresh the div every X seconds using AJAX (javascript).
PachinSV
  • 3,680
  • 2
  • 29
  • 41