phdphd Posted September 22, 2018 Share Posted September 22, 2018 Hi All, I have a php file with some JS that implements a map with markers on it. It works. However to make my php file cleaner, I want to move the JS that builds the map and markers to a seperate js file. In the initial PHP file, the JS code has a line like this for the coordinates, that I need to adapt for a standalone JS file. var addressPoints = [<?php echo implode(",", $coordinates); ?>]; So I created a hidden div to echo the coordinates before storing them into a JS variable. I did the following : <div id="dom-target-coord" style="display: none;"> <?php $outputcoord = implode(",", $coordinates); echo htmlspecialchars($outputcoord); ?> </div> <script> var div = document.getElementById("dom-target-coord"); var myDataCoord = div.textContent; </script> <script src="../js/map.js"></script> And the first line of the map.js file is var addressPoints = [myDataCoord]; This does not work. In there something wrong in the syntax of this line ? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/307712-map-markers-do-not-show/ Share on other sites More sharing options...
phdphd Posted September 22, 2018 Author Share Posted September 22, 2018 Just solved by using : var addressPoints = JSON.parse("[" + myDataCoord+ "]"); Question : why was this not necessary in the initial PHP file ? Quote Link to comment https://forums.phpfreaks.com/topic/307712-map-markers-do-not-show/#findComment-1561008 Share on other sites More sharing options...
requinix Posted September 22, 2018 Share Posted September 22, 2018 Think about how it works. Your original form will create output that looks like [1, 2, 3, ...] which is an array with however-many elements. The new form is [variable] which is an array with one element, whose value is a string. [1, 2, 3] versus ["1, 2, 3"]. But what you have now really isn't the way to go about it. You say you wanted to move the map stuff to another file? How so? Quote Link to comment https://forums.phpfreaks.com/topic/307712-map-markers-do-not-show/#findComment-1561017 Share on other sites More sharing options...
phdphd Posted September 22, 2018 Author Share Posted September 22, 2018 Thanks for your explanation. Now it works correctly again. I just put the JS staff that was initially inserted in the PHP file in a separate JS file that now contains the updated "var addressPoints" line. Quote Link to comment https://forums.phpfreaks.com/topic/307712-map-markers-do-not-show/#findComment-1561018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.