Home php Passing data from php to javascript

Passing data from php to javascript

Author

Date

Category

There is a php file with the code:

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

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

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

Answer 1, authority 100%

They can be passed to JS in one of the following ways:

  • via GET parameters, if the document is referenced from somewhere
  • embedding the value of variables in the DOM document
  • by loading via AJAX from the server, for example, by generating a JSON response
  • via cookie
  • generating JS code using PHP

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

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

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

var mylat = $ ('div.hidden'). data ('lat');
var mylong = $ ('div.hidden'). data ('lng');

Answer 2, authority 43%

You fail because by the time you try to access from a js file on the user’s machine to the variables located on the server , then these variables are already “dead” . PHP worked, gave the data to the user, and no longer knows what is happening with the page in the user’s browser.
I would start a file like vars.php :

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

In markup:

& lt; html & gt;
& lt; head & gt;
& lt; script type = "text / javascript" src = "scripts / vars.php" & gt; & lt; / script & gt;
& lt; script type = "text / javascript" src = "scripts / load_map_step.js" & gt; & lt; / script & gt;
& lt; / head & gt;
& lt; body & gt;
& lt; / body & gt;
& lt; / html & gt;

Answer 3, authority 14%

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

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

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

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions