简体   繁体   English

将PHP变量传递给Jquery

[英]Passing PHP-Variables to Jquery

i want to load some data from an sql-table and use them with jquery to color a map. 我想从sql表中加载一些数据,并将它们与jquery一起使用以为地图着色。

I picked up the data with PHP: 我用PHP提取了数据:

<?php
  include 'connect.php';
  session_start();
  $userid = $_SESSION['userid'];

  $sql = "
  SELECT landstatus.shortland
  FROM landstatus         
  WHERE users_id='1' 
  AND status ='wanttovisit'
  ";

  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
          echo $row["shortland"]. "<br>";
      }
  } 
  $conn->close();
?>

Now i want to use shortland for the static value 'ca' in jquery: 现在我想在jQuery中使用shortland作为静态值“ ca”:

<script>
  jQuery(document).ready(function () {
      jQuery('#vmap').vectorMap({
      map: 'world_en',
      backgroundColor: '#333333',
      color: '#FFFFFF',
      hoverOpacity: 0.7,
      selectedColor: '#727272',
      enableZoom: true,
      colors:{
              'ca' : '#4E7387',
          },
          series: {
            regions: 
            [{
              attribute: 'fill'
            }]
          },

      onRegionClick: function (element, code, region) {
        $(".info-box-region").append(region);
        $(".info-box").show();
        $("input[name=region]").val(region);
        $('input[name=code]').val(code);
      }

    });
  });
</script>

At the end colors would be filled with all shortlands from the database - each shortland has the same hex-code. 最后,将用数据库中的所有短地填充颜色-每个短地具有相同的十六进制代码。

Thx for helping me. 感谢您的帮助。

Place all shortland values in an array and loop through it to create the ca entries 将所有短地值放置在数组中并循环遍历以创建ca条目

  $result = $conn->query($sql);

  $shortlands = array();
  if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
        $shortlands[] = $row["shortland"];
     }
  }

Then loop over the values. 然后循环这些值。 eg 例如

colors:{
<?php
foreach ($shortlands as $val) {
   echo "'{$val}': '#4E7387',\n";
}
?>
},

You can "pass" anything PHP knows into JavaScript variables simply by echoing them into JavaScript code in your template: 您只需将PHP知道的任何内容回显到模板中的JavaScript代码中,就可以将它们“传递”到JavaScript变量中:

<script>
    var myJavaScriptVar = <?php echo $myPhpVar ?>
</script>

Just do this: 只要这样做:

<script>
  jQuery(document).ready(function () {
      jQuery('#vmap').vectorMap({
      map: 'world_en',
      backgroundColor: '#333333',
      color: '#FFFFFF',
      hoverOpacity: 0.7,
      selectedColor: '#727272',
      enableZoom: true,
      colors:{
              'ca' : '<?php echo $row["shortland"] ;?>',
          },
          series: {
            regions: 
            [{
              attribute: 'fill'
            }]
          },

      onRegionClick: function (element, code, region) {
        $(".info-box-region").append(region);
        $(".info-box").show();
        $("input[name=region]").val(region);
        $('input[name=code]').val(code);
      }

    });
  });
</script>

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

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