Loading large values of data from php to javascript? -


i read question php array loading javascript see can load large amount of data php javascript, seems may have implemented wrong. javascript processes , formats data after comes in php loads data database, data placed client-side session storage data can worked each page. (if there better way please let me know).

this in 1 .php file.

<?php  session_start();  require_once 'classes/membership.php';  $membership = new membership();  $confirmation = $membership->confirm_membership();  if ($confirmation){      $data = $membership->get_data("assump");      echo '<script>var data = '.json_encode($data) .';</script>';  }  ?> 

this in separate .js file

function loaddata(){      // sessionstorage can accessed javascript file      for(var = 0; < data.length; i++){          sessionstorage.setitem("assump" + i, data[i]);      }  } 

however no values being loaded. possible do?

edit: moved javascript .php file var data being created php script @ top of file, placed function loaddata() script tag after body tag in html.

this should work fine. may experience problem scope (not 100% sure). , here suggestion.

attach variable window object. that's everywhere , can access everywhere.

echo '<script>window.mydata = '.json_encode($data) .';</script>'; 

ideally include line before other scripts included, should fine. not sure session storage because interact data directly anywhere:

window.alert(window.mydata.blah...); 

i've found assigning global variables, window object know there no scope problems , it's there you.

you can check it's existence using:

if(window.mydata!=undefined){ ... } 

or following above implies checking exists below not true if window.mydata exist set false or 0 or that...

if(window.mydata){ ... } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -