简体   繁体   English

从php数组获取javascript变量值

[英]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我的 javascript 正在地图上填充标记,为此我需要位置,这是我按照下面给出的格式从 php 文件生成的
  2. i want to dynamically get the result of the php array in the javascript array我想在javascript数组中动态获取php数组的结果
      <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 文件是这个

    <?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.您将需要使用 ajax 调用。 Here is a question on Stack Overflow that has some examples of an ajax call to a php page. 是 Stack Overflow 上的一个问题,其中有一些 ajax 调用 php 页面的示例。

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.请记住,您需要包含一个 jQuery 库来执行此操作,或者您可以按照此问题进行操作以在没有 jQuery 的情况下进行 ajax 调用。

You could use PHP to output the variables into a hidden input field's value attribute and encode them as JSON.您可以使用 PHP 将变量输出到隐藏输入字段的value属性中,并将它们编码为 JSON。 Then, using Javascript use JSON.parse to decode the PHP variables from the hidden input field's value attribute.然后,使用 Javascript 使用JSON.parse从隐藏输入字段的 value 属性解码 PHP 变量。 You can then remove the element from the DOM.然后,您可以从 DOM 中删除该元素。

The trick is by echoing the PHP var into javascript.诀窍是将 PHP 变量回显到 javascript 中。 As Php is processed before, outputting correct Js.由于之前处理过Php,输出正确的Js。

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

In this case $VAR must be something like $VAR = "'barcelona','madrid','london'";在这种情况下,$VAR 必须类似于 $VAR = "'barcelona','madrid','london'"; Adjust $VAR so it generate correct output.调整 $VAR 使其生成正确的输出。 No need to use JSON encode if you just need it for this.如果您只需要它,则无需使用 JSON 编码。

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

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