Passing a php variable to javascript inside php function that echos js code -


i'm coding plug-in wp , i'm writing javascript script head of page document using php's echo. need pass php variables script. tried json.encode() not pass right thing here function:

function test_action() {     global $wpdb;     $contents = $wpdb->get_results("select content wp_map_user_demo");     $lats = $wpdb->get_results("select latitude wp_map_user_demo");     $longs = $wpdb->get_results("select longitude wp_map_user_demo");      //modifying header file of wp     echo '<title>demo</title>             <meta name="viewport" content="initial-scale=1.0, user-scalable=no">             <meta charset="utf-8">             <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=mykey"></script>';     //including custom google maps script     echo "<script type='text/javascript'>                     var map;                     var contents = ". json_encode($contents) .";                     var lats = ". json_encode($lats) . ";                     var longs = " .  json_encode($longs). ";                      document.write('<p>' + lats[0] + '</p>');                     function initialize() {                     var mapoptions = {                       center: new google.maps.latlng(lats[0], longs[0]),                       zoom: 8                     };                     var map = new google.maps.map(document.getelementbyid('map'),                         mapoptions);                   }                   //map.setoptions({draggable: true});                   google.maps.event.adddomlistener(window, 'load', initialize);             </script>"; } 

when open page on wp document.write('<p>' + lats[0] + '</p>'); outputs [object object] on page instead of latitude data.i checked php variables , correct, guess problem javascript usage.

thanks helps...

try ;)

function test_action() {     global $wpdb;     $results = $wpdb->get_results("select content,latitude,longitude wp_map_user_demo");      $contents = $lats = $longs = array();      foreach($results $result) {         $contents[] = $result->content;         $lats[] = $result->latitude;         $longs[] = $result->longitude;     }      //modifying header file of wp     echo '<title>demo</title>             <meta name="viewport" content="initial-scale=1.0, user-scalable=no">             <meta charset="utf-8">             <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=mykey"></script>';     //including custom google maps script     echo "<script type='text/javascript'>                     var map;                     var contents = " . json_encode($contents) . ";                     var lats = " . json_encode($lats) . ";                     var longs = " . json_encode($longs) . ";                      document.write('<p>' + lats[0] + '</p>');                     function initialize() {                     var mapoptions = {                       center: new google.maps.latlng(lats[0], longs[0]),                       zoom: 8                     };                     var map = new google.maps.map(document.getelementbyid('map'),                         mapoptions);                   }                   //map.setoptions({draggable: true});                   google.maps.event.adddomlistener(window, 'load', initialize);             </script>"; } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -