dongzhong5756
2016-02-18 21:44
浏览 53

将PHP变量传递给Jquery

i want to load some data from an sql-table and use them with jquery to color a map.

I picked up the data with 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:

<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.

图片转代码服务由CSDN问答提供 功能建议

我想从sql表中加载一些数据,并使用jquery为地图着色。

我用PHP获取数据:

 &lt;?php 
 include'connect.php'; 
 session_start(); \  n $ userid = $ _SESSION ['userid']; 
 
 $ sql =“
 SELECT landstatus.shortland 
 FROM landstatus 
 WHERE users_id ='1'
 AND status ='wanttovisit'
”;  
 
 $ result = $ conn-&gt; query($ sql); 
 
 if if($ result-&gt; num_rows&gt; 0){
 //输出每行的数据
 while($ row  = $ result-&gt; fetch_assoc()){
 echo $ row [“shortland”]。  “&lt; br&gt;”; 
} 
} 
 $ conn-&gt; close(); 
?&gt; 
   
 
 

现在我想要 在jquery中使用shortland作为静态值'ca':

 &lt; script&gt; 
 jQuery(document).ready(function(){
 jQuery('#vmap)  ')。vectorMap({
 map:'world_en',
 backgroundColor:'#333333',
 color:'#FFFFFF',
 hoverOpacity:0.7,
 selectedColor:'#727272',
 enableZoom  :true,
 colors:{
'ca':'#4E7387',
},
系列:{
 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); 
} 
 
}); \  n}); 
&lt; / script&gt; 
   
 
 

最后颜色将填充数据库中的所有短地 - 每个短地都有相同的h ex-code。

Thx帮助我。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douba4933 2016-02-18 22:05
    已采纳

    Place all shortland values in an array and loop through it to create the ca entries

      $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. e.g.

    colors:{
    <?php
    foreach ($shortlands as $val) {
       echo "'{$val}': '#4E7387',
    ";
    }
    ?>
    },
    
    打赏 评论
  • douji6461 2016-02-18 21:57

    You can "pass" anything PHP knows into JavaScript variables simply by echoing them into JavaScript code in your template:

    <script>
        var myJavaScriptVar = <?php echo $myPhpVar ?>
    </script>
    
    打赏 评论
  • dougutuo9879 2016-02-18 21:58

    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>
    
    打赏 评论

相关推荐 更多相似问题