There is a php file with the code:

<? $Name = $line['Name']; $Adres = $line['Adres']; $Lat = $line['Lat']; $Lng = $line['Lng']; ?> <html> <head> <script type="text/javascript" src="scripts/load_map_step.js"></script> </head> <body> </body> </html> 

Those. in php, I assigned values ​​to variables, and now I want to access them from the file load_map_step.js. I try so but nothing works, the data is empty ...

 var mylat = $(Lat).val(); var mylong = $(Lng).val(); 

    3 answers 3

    They can be transferred to JS in one of the ways:

    • via GET-parameters, if you refer to the document from somewhere
    • embed the value of the variables in the DOM document
    • loading via AJAX from the server, for example, by generating a JSON response
    • via cookie
    • generating js code using PHP

    The simplest case is the transfer of variables through the DOM, you can put them for example in the data-attribute of some hidden div document

     <?php $Name = $line['Name']; $Adres = $line['Adres']; $Lat = $line['Lat']; $Lng = $line['Lng']; ?> <html> <head> <script type="text/javascript" src="scripts/load_map_step.js"></script> </head> <body> <div class='hidden' data-name='<?= $Name ?>' data-address='<?= $Adres ?>' data-lat='<?= $Lat ?>' data-lng='<?= $Lng ?>' ></div> </body> </html> 

    Then if you use jQuery, you can access these attributes through the data () method

     var mylat = $('div.hidden').data('lat'); var mylong = $('div.hidden').data('lng'); 
    • IMHO, almost perfect answer. It remains only to add json_encode. - Evgeny Borisov

    You do not succeed because by the time when you try to access from the js file on the user 's machine, the variables that are on the server , then these variables are already "dead" . PHP worked, gave the data to the user, and already know he doesn’t know what is happening with the page in the browser of the user.
    I would start a file, for example vars.php :

     <? Header("Content-type: application/javascript"); ?> var Name = <?= $line['Name'];?> var Adres = <?= $line['Adres'];?> var Lat = <?= $line['Lat'];?> var Lng = <?= $line['Lng'];?> 

    In the markup:

     <html> <head> <script type="text/javascript" src="scripts/vars.php"></script> <script type="text/javascript" src="scripts/load_map_step.js"></script> </head> <body> </body> </html> 
    • one
      brought vars.php , and add src="scripts/vars.js" - Grundy
    • @Grundy: Corrected. - user200141
    • Interesting ... Really! - Alexander
    • So what is the final file you need to create vars.php or vars.js ??? - Alexander
    • You need to get php and use it to generate js code, roughly how to generate html code - user200141

    Since php is executed on the server and js is executed on the client, one of the ways to get the value of variables is simply to output them in a script block, for example,

     <? $Name = $line['Name']; $Adres = $line['Adres']; $Lat = $line['Lat']; $Lng = $line['Lng']; ?> <html> <head> <script> var Lat='<?= $Lat ?>'; </script> <script type="text/javascript" src="scripts/load_map_step.js"></script> </head> <body> </body> </html> 

    Now at the moment of the script execution, a global variable will be declared, which will also be available inside the loadable script.

    An additional option: add server settings so that js files are also processed as php.