简体   繁体   English

如何将.son中的json_encoded变量传递给外部.js?

[英]How to pass json_encoded variables from .php to external .js?

that's my first post :) I have a problem with passing json_encoded variables from PHP VIEW file to an external JS. 这是我的第一篇文章:)我有一个问题,从PHP VIEW文件传递json_encoded变量到外部JS。 I am using FuelPHP. 我正在使用FuelPHP。 The following is part of the VIEW: 1. These are the PHP variables: 以下是VIEW的一部分: 1。这些是PHP变量:

<?php
  $sensor_id_num = $sensor->id_num; 
  $sensor_name = $sensor->name;
  $sensor_unit = $sensor->unit;
  $sensor_lati = $sensor->lati;
  $sensor_longi = $sensor->longi;
?>

2. Here the variables are json_encoded and their value is given to JS vars: 2.这里的变量是json_encoded,它们的值被赋予JS变量:

<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
  var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
  var sensor_name = <?php echo json_encode($sensor_name); ?>;
  var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
  var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
  var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>

3. The above mentioned mapmarkers.js is the external JS that I want to pass the vars to. 3.上面提到的mapmarkers.js是我想传递vars的外部JS。 In that JS I am using google.maps javascript API to draw a map and one marker for each map. 在那个JS我使用google.maps javascript API为每个地图绘制一个地图和一个标记。 Every marker is representing a sensor's location, so that's why I'm passing latitude and longitude. 每个标记都代表一个传感器的位置,这就是我通过纬度和经度的原因。 That should be part of the php VIEW. 这应该是php VIEW的一部分。 That View shows some sensor's text info along with the map. 该视图显示了传感器的一些文本信息以及地图。

4. So the text info and the map are visualized, but not the markers. 4.因此,文本信息和地图可视化,而不是标记。 The problem is in the JS file. 问题出在JS文件中。 When I try to use the vars from tag into the JS, the browser console shows their value is 'undefined'. 当我尝试将标签中的变量用于JS时,浏览器控制台显示其值为“未定义”。 I'm accessing the vars with 'window.name_of_var'. 我正在使用'window.name_of_var'访问变量。 Even when I access them without 'window.' 即使我在没有“窗口”的情况下访问它们。 their value is not shown, "Uncaught ReferenceError: sensor_lati is not defined" is shown instead. 它们的值未显示,而是显示“Uncaught ReferenceError:sensor_lati未定义”。 That's part of the JS: 这是JS的一部分:

var myLatLng = new google.maps.LatLng(window.sensor_lati,window.sensor_longi);

var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        title: window.sensor_name,
        html: window.sensor_name

});
google.maps.event.addListener(marker, 'click', function () {
      infowindow.setContent(html);
      infowindow.open(map, marker);
});

Does anybody has an idea where could the problem be? 有没有人知道问题在哪里? I don't have much experience with FuelPHP and JavaScript. 我对FuelPHP和JavaScript没有多少经验。 Any help would be appreciated ;) 任何帮助,将不胜感激 ;)

You cannot have a <script> element with a src attribute and javascript code within it. 你不能拥有一个带有src属性 javascript代码的<script>元素。 Well, you can, but it's pretty much undefined what browsers do with that so your results will vary from browser to browser. 好吧,你可以,但它几乎没有定义浏览器的功能,所以你的结果会因浏览器而异。

The solution would be to first define the variables, then include the remote script: 解决方案是先定义变量,然后包含远程脚本:

<script type="text/javascript">
    var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
    var sensor_name = <?php echo json_encode($sensor_name); ?>;
    var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
    var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
    var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript"></script>

Set the variables before calling the external js file 在调用外部js文件之前设置变量

<script type="text/javascript">
  var sensor_id_num = <?php echo json_encode($sensor_id_num); ?>;
  var sensor_name = <?php echo json_encode($sensor_name); ?>;
  var sensor_unit = <?php echo json_encode($sensor_unit); ?>;
  var sensor_lati = <?php echo json_encode($sensor_lati); ?>;
  var sensor_longi = <?php echo json_encode($sensor_longi); ?>;
</script>
<script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript">
  1. You should divide your <script /> into 2 parts. 您应该将<script />分成两部分。
  2. Probably you don't need json_encode php function here. 可能你在这里不需要json_encode php函数。 Also add wrapping doublequotes. 还要添加包装双引号。
  3. mapmarkers.js must be added after defining your variables 定义变量后必须添加mapmarkers.js

 <script type="text/javascript"> window.sensor_id_num = "<?php echo $sensor_id_num; ?>"; window.sensor_name = "<?php echo $sensor_name; ?>"; window.sensor_unit = "<?php echo $sensor_unit; ?>"; window.sensor_lati = "<?php echo $sensor_lati; ?>"; window.sensor_longi = "<?php echo $sensor_longi; ?>"; </script> <script src="<?php echo Asset::get_file('mapmarkers.js','js') ?>" type="text/javascript" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM