views:

72

answers:

2

i have this code

 var myChart = new FusionCharts("../themes/clean/charts/hbullet.swf", "myChartId", "400", "75", "0", "0");
      myChart.setDataXML("<chart lowerLimit='0' upperLimit='100' caption='Revenue' subcaption='US $ (1,000s)' numberPrefix='$' numberSuffix='K' showValue='1'><colorRange><color minValue='0' maxValue='50' color='A6A6A6'/><color minValue='50' maxValue='75' color='CCCCCC'/><color minValue='75' maxValue='100' color='E1E1E1'/></colorRange><value>78.9</value><target>80</target></chart>");
      myChart.render("chartdiv");

now i want to edit some values within this code.

however to make things more complex im using a php function to write the "chartdiv" div and i need database values placed into this code. is it possible to slide php variables from my php function into this .js file? or is it better or even possible to write the javascript with PHP?

+6  A: 
  • You can write the javascript with PHP in the file itself (inline javascript).
  • You can refer to a php file within your <script src='thejs.php'> and have the script output javascript.

Personally, I like to put the variables in a structure (say, an array) and then convert it to JSON. This way, you can easily add and remove variables, and check their existance in JS. JSON is an easy way to have both languages communicate.

Konerak
A: 

You cant write PHP into your .JS file. If its used in multiple places why dont you chuck into a php include file and call it in when you need to, the js code will be inline though. You can then update the values you need with PHP.

PHPology