douao3063 2010-10-07 05:44
浏览 140

如何在js文件中编写php代码

Is my program i divide the pages as a div in the first div i added this code

  <?php
    $table="am_users";
    $query="select distinct(`user_email`) from $table";
    $result=mysql_query($query);
    $num_rows = mysql_num_rows($result);
     while($data1=mysql_fetch_array($result))
     {
     $data[]=$data1['user_email'];
     }
     sort($data);  
     foreach($data as $search_term) 
     { 
     $js_data[] ="\"" . $search_term . "\""; 


    }  
<script type="text/javascript">
var collection = [<?php echo implode($js_data, ","); ?>]; 
</script>   
     ?> 

below this i include my css file but division 1 is working in division 2 the css is not applied . This Problem is due to the div 1 had Js variable value If I remove the var collection = []; statement then the css working fine . So Is It possible to pass the value from PHP file to JS file On Loading Time

  • 写回答

4条回答 默认 最新

  • doujiayuan8415 2010-10-07 05:47
    关注

    Save the file as .php instead of .js and add

    Header("content-type: application/javascript");
    

    . at the beginning of the file. After that you will be able to link to the file as

    <script type="text/javascript" src="youfile.php"></script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值