最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to write the php code in js file - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Oct 7, 2010 at 7:40 Meena asked Oct 7, 2010 at 5:44 MeenaMeena 9675 gold badges19 silver badges35 bronze badges 1
  • What do you mean by write in the JS file? Do you want to convert this PHP code to JavaScript, do you want to use JavaScript to call PHP, or...? – BoltClock Commented Oct 7, 2010 at 5:46
Add a comment  | 

4 Answers 4

Reset to default 10

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>

what you need is to call to file with php extension

like

<script type="text/javascript" src="/includes/slideshow.js.php"></script>

and in the beginning of php file

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

look the example in http://www.givegoodweb.com/post/71/javascript-php

Save the file named abc.js to abc.js.php, hence server will come to know that it contains some php question, so that it will process first php content then replace the result in .js file and send to browser, By doing this the .js file will contain dynamic contents..

In the questions code i see that the value of a JS var is being set from PHP using echo. What I need to do is the other way around. That is store values of JS vars into PHP var properties. Is it possible ? I am able to include the JS file as PHP, but not able to pass the value from JS to PHP var. PS - the values from JS come from a AJAX call, so I need to update the values regularly into PHP var..

发布评论

评论列表(0)

  1. 暂无评论