Skip to content
Advertisement

Javascript: Refresh current page after event change $_SESSION in a other page

I need help for a subtlety. I created a multilingual site without changing using the same php file, I just modify its content. For this I use sessions. So I configured 2 buttons: FR and EN which change the language. I go through another php file. With this code it works if I manually reload the page. But if I use:

JavaScript

it doesn’t work and i want to automatically reload the page without user intervention. Does anyone have the solution? Thanking you, Regards,

my code index.php :

JavaScript

change.php :

JavaScript

Advertisement

Answer

Your code reloads the index.php immediately and doesn’t even wait for the request (to change.php) to be sent.

You can handle the onreadystatechange event of XMLHttpRequest, specifically you’re looking for state 2, which is HEADERS_RECEIVED, or any later state.

When the PHP script sends headers, it means it already finished executing (and set the session). And you can safely reload the index page.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement