that's my first post :) I have a problem with passing json_encoded variables from PHP VIEW file to an external JS. I am using FuelPHP. The following is part of the VIEW: 1. These are the PHP variables:
<?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:
<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. In that JS I am using google.maps javascript API to draw a map and one marker for each map. 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. 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. The problem is in the JS file. When I try to use the vars from tag into the JS, the browser console shows their value is 'undefined'. I'm accessing the vars with '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. That's part of the 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. Any help would be appreciated ;)
<script> // all json here </script>then<script src="googlemaps"/>