如何在js文件中编写php代码 [英] How to write the php code in js file
问题描述
我的程序是否将页面划分为第一个div中的div我添加了此代码
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>
?>
此下我包括我的css文件
但是第1分部正在工作除法2不适用css。这个问题是由于div 1有Js变量值如果我删除了
var collection = [];
语句然后css工作正常。因此可以将值从PHP文件传递到JS文件加载时间
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
推荐答案
将文件另存为.php而不是.js并添加
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>
这篇关于如何在js文件中编写php代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!