douna6802
douna6802
2014-05-30 19:26

如何将php js变量改为jquery文件

  • php
  • javascript
  • jquery

how i can parse an Variable (php with js script) to an js file (jquery)?

This is what i do:

<script type='text/javascript'>
    <?php
    session_start();
     $var1 = $_SESSION['one'];
     $var2 = $_SESSION['two'];
      $con = mysqli_connect("");
      if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();}

      $result = mysqli_query($con,"Select name FROM  table WHERE one < '". $var1 . "' OR two < '". $var2 . "'");
      $rows=array();

      while($row=mysqli_fetch_array($result))
      {
      $rows[]=$row["name"];
      }

      $js_array = json_encode($rows);
      echo "var stat = ". $js_array . ";
";

       ?>

      </script>

In my js file i have this:

$(document).ready(function($)
{
for (var x = 0; x < stat.length; x++) {

    $(".class#" + stat[x]).css("background-color", "#e74c3c");

    alert(stat);
}});

My browser show this :

var stat = ["a1","a2","a3"];

How i can now get the php js_array to the stat variable (js)?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐