0

Let's say I have a PHP file that contains a POST or GET variable:

if(isset($_POST['data'])){
    $data = $_POST['data'];
    echo $data;
}

Is it possible to update that value, While the user is viewing that page?

So that when I (The admin) click a button on another page, A value is changed in the DB and that variable or an html element is changed?

So for example (Didn't work):

$.post('file.php', {data: data}, function(){});

Then while the user is viewing that file.php, The $data is updated with the new posted data.

IncredibleHat
  • 4,000
  • 4
  • 15
  • 27
  • How should that work? Once the browser receives your markup, the server is finished doing its work. – Nico Haase Aug 06 '18 at 15:44
  • Its not really clear what you are actually asking, can you have a go at making it more clear – RiggsFolly Aug 06 '18 at 15:47
  • Make an xhr request and use the callback to change the dom – Kisaragi Aug 06 '18 at 15:48
  • Look into [https://socket.io/](https://socket.io/) – IncredibleHat Aug 06 '18 at 15:48
  • @RiggsFolly, I want to update something on the page the user is viewing, Like an html element with a specific data to be changed to other data –  Aug 06 '18 at 15:49
  • @IncredibleHat, Does it support PHP? –  Aug 06 '18 at 15:50
  • It supports whatever server language you work in. – IncredibleHat Aug 06 '18 at 15:50
  • @Kisaragi, Could you show me an example, please?, I could do the XHR, But not sure about the callback –  Aug 06 '18 at 15:51
  • 3
    You cannot do this simply, you are going to have to do some AJAX or SOCKET coding – RiggsFolly Aug 06 '18 at 15:51
  • 2
    As @RiggsFolly said... this is the kind of thing that is not for the faint of heart, and requires a big commitment to get working to allow 'pushing' data to clients, instead of them doing basic 'requests'. – IncredibleHat Aug 06 '18 at 15:52
  • @RiggsFolly, Could ajax do that? changing a page a user is viewing? –  Aug 06 '18 at 15:54
  • @RiggsFolly, The user id not doing an action, It's the admin who clicks a button –  Aug 06 '18 at 15:54
  • 1
    It could, but involves timers etc and a lot of wasted IO, but it is not the best method. For that I would suggest sockets and push notification – RiggsFolly Aug 06 '18 at 15:54
  • 1
    The only way to do it with ajax, is to employ [short polling](https://stackoverflow.com/questions/4642598/short-polling-vs-long-polling-for-real-time-web-applications)... which can be tasking, and if not managed right, slam your server with a self-imposed DDoS :) – IncredibleHat Aug 06 '18 at 15:55
  • @RiggsFolly, So for example if a user just registered and there is a `div` with a message "waiting for admin approval", Then when the admin clicks approve on the dashboard, That `div` is changed to "Your account is approved now" –  Aug 06 '18 at 15:56
  • Put like that it sounds trivial but it is not. Afraid you will need to do some research – RiggsFolly Aug 06 '18 at 15:59
  • 1
    "Then when the admin clicks approve on the dashboard, That div is changed to "Your account is approved now"...if this is a human-controlled task, will it really happen so fast that the user will wait on the page? Why not just send them an email when it's approved? Then they can visit the site again and log in, at their convenience. – ADyson Aug 06 '18 at 16:03
  • @ADyson, I'm giving an example –  Aug 06 '18 at 16:12
  • Well at least give us a practical one which might make sense, then we know you're not barking up the wrong tree entirely. – ADyson Aug 06 '18 at 16:14
  • @ADyson, When the user orders something and the admin accepts that order, An element on the user page would be changed without refreshing the page –  Aug 06 '18 at 16:20
  • again, the human admin can really do it so fast that the user is still sitting there waiting for it? People don't hang around on web pages for more than a minute or two, at the very most. They expect to get on with something else instead of waiting for a div to be updated. I mean, you could do this, sure, it might help some people, but don't expect the user to always be there. Send some other notification, e.g. email, as well. – ADyson Aug 06 '18 at 18:26

0 Answers0