简体   繁体   中英

getting javascript variable value from php array

  1. My javascript is populating markers on map and for that i need locations ,which i have generated from php file in the format given below
  2. i want to dynamically get the result of the php array in the javascript array
      <script type="text/javascript">
        var locations = /// i want here the output as shown below from the php file

    var locations = [["Vidyasagar","28.6139391","77.20902120000005"],
         ["Pushpadantsagar","21.4598","80.195"],
         ["Tarunsagar","28.638","77.2936"],
         ["Samyaktbhushan","20.593684","78.96288000000004"],
         ["Pavitrasagar","23.2836","79.2318"],
         ["Prayogsagar","23.2836","79.2318"],
         ["Arunsagar","30.016","77.4"]];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 7,
      center: new google.maps.LatLng(23.2836,79.2318),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    </script>

and the php file is this

    <?php
    $servername = "localhost";
    $username = "root"; 
    $password = "";
    $dbname = "jainmunilocator";
    $connection=mysqli_connect($servername, $username, $password, $dbname);;
    if (!$connection) {
         die("Connection failed: " . mysqli_connect_error());
    }
    $array = array();
    $sql = "SELECT name,id,lat,lng FROM muni_location,munishri WHERE mid=id AND lat<>0";
    $result=mysqli_query($connection,$sql);
    $i=0;

    while($row = mysqli_fetch_assoc($result)){
        if(isset($row)){
            $array[$i][0]=$row['name'];
            $array[$i][1]=$row['lat'];
            $array[$i][2]=$row['lng'];
            $i++;
        }
    }
    echo json_encode($array);
    ?>

You will need to use an ajax call. Here is a question on Stack Overflow that has some examples of an ajax call to a php page.

Keep in mind though that you'll need to either include a jQuery library for you to do that, or you can follow along in this question to make an ajax call without jQuery.

You could use PHP to output the variables into a hidden input field's value attribute and encode them as JSON. Then, using Javascript use JSON.parse to decode the PHP variables from the hidden input field's value attribute. You can then remove the element from the DOM.

The trick is by echoing the PHP var into javascript. As Php is processed before, outputting correct Js.

<script>
   var locations = array(<?= $VAR ?>);
</script>

In this case $VAR must be something like $VAR = "'barcelona','madrid','london'"; Adjust $VAR so it generate correct output. No need to use JSON encode if you just need it for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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