0

I have an html page calling a php page as shown below. It refreshes every 10 seconds. The menu php page has a set of divs with toggleable visibility inside of it. The number of divs and their ids are determined dynamically by reading from a database. Every time the php section refreshes, the divs automatically become invisible which is annoying. I was wondering if there was a way that in between refreshes I could tell my php page which divs were previously visible and which divs were not.

function refresh (){
var menu=new XMLHttpRequest(); 

menu.onreadystatechange=function(){ 

if (menu.readyState==4 && menu.status==200){ 
    document.getElementById("menuDiv").innerHTML=menu.responseText; 
   } 
} 
menu.open("GET","menu.php",true); 
menu.send();
}
William Roberts
  • 319
  • 2
  • 5
  • 21

1 Answers1

0

You could try using localStorage like this.

localStorage["test"] = menu.responseText;

Use it like so

document.getElementById("menuDiv").innerHTML = localStorage["test"];

More about localStorage here. http://www.w3schools.com/html/html5_webstorage.asp

This works for newer browsers, but if you want to support older browsers as well, you will need to use cookies.

http://www.w3schools.com/php/func_http_setcookie.asp

If you need to store the information on the server side, you could use session variables. More on that here.

http://www.w3schools.com/php/php_sessions.asp

Caimen
  • 2,623
  • 2
  • 26
  • 43
  • Can I pass localstorage, cookie, or session values from the html file to be used within the php file? – William Roberts Nov 18 '15 at 18:35
  • You can query query strings and $_GET['test'] to pass back and forth between the server and client. http://php.net/manual/en/reserved.variables.get.php – Caimen Nov 18 '15 at 19:00
  • The localStorage on the javascript in the html file and the $_GET in the php file are the same array? – William Roberts Nov 18 '15 at 20:03
  • Check out the answer here for more specific code. http://stackoverflow.com/questions/16042565/how-can-i-pass-saved-localstorage-web-data-to-a-php-script localStorage can only store strings. Also javascript and php don't really interact in the way you are suggesting, you can only pass information in various ways such as POST or GET or in the case of server to client printing out variables inside javascript. You might also refer to the answer here on the differences between server and client scripting http://stackoverflow.com/questions/6369313/difference-between-javascript-and-php – Caimen Nov 18 '15 at 20:18