Passing data from one page to another and refresh the main page

Hello! I have a small problem that gives me headaches already.

On a PC there is a webpage opened that streams a video. On the same PC an user can access a file (admin.php) and choose what type of stream the page should show: live or prerecorded video. The admin.php file is accessed trough a smartphone. When the type of streaming is changed the main page should refresh itself automatically.

Basically, when a change is made in MySQL the main page should refresh.

Any help will be appreciated!

EDIT. I’ve edited the initial question.

Hi @yenoH! Provided both pages are on the same domain, you can write changes on “back office” to the local storage, and listen to its changes on “main page” like this:

// Main page listens to changes
window.addEventListener('storage', function(event) {

    // Refresh page content or something
    console.log(event.key + ': ' + event.oldValue + ' => ' + event.newValue);
});

// Back office changes local storage
localStorage.myValue = 'some initial value';
localStorage.myValue = 'some new value';

// => Console output on main page is
// myValue: null => some initial value 
// myValue: some initial value => some new value

Thank you for your quick reply. I will try this. In the meantime a new idea popup in my head. To create a button in the BO that sends a variable to the main page. When that variable is received, the main page will be refreshed. It’s that possible?

D’oh, I only just noticed that you posted this on the PHP board… ^^ my code above is pure JS and probably doesn’t really solve your reformulated question. Anyway, the main page can’t refresh itself automatically with PHP alone since PHP can only respond to requests from the client; thus you’d have to send continuous requests using AJAX.

As long as both pages are opened in the same browser, you could use JS to access the local storage as described above. All instances of your application have full access to the very same Storage object, so you wouldn’t even have to pass data around – instance A might write, while instance B might listen to change events (and in turn send another request to the server, if desired).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.