Solved

pass php variable to js file

Posted on 2008-10-15
4
1,152 Views
Last Modified: 2013-12-13
Hi.
I'm getting data from a database using php. I need to use the data I got from the DB in javascript function
that is stored in another js file. (I'm generating a bar char using java script functions there)

how can I pass variable from php file to a js file and set a global variable in the js file with the variable the was received from the php file?

or , how can I call js function from the php file ?
0
Comment
Question by:egrolpe
4 Comments
 
LVL 4

Accepted Solution

by:
webopius earned 500 total points
ID: 22722999
Because PHP is running on the server and Javascript runs on the client once PHP has run and the page has been prepared on the server, it should be relatively easy to pass the value of a PHP variable to Javascript.

I've included a prototype code snippet below that I think does what you describe...

If you are creating BarCharts in using Javascript, I can highly recommend the Google gadgets and visualization API.
<?php 
// The PHP code 
  $phpdb_title = DBquery1();
  $phpdb_value = DBquery2();
//etc..
?>
 
<script type="text/javascript">
// The Javascript code 
function drawChart() {  
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Label');
  data.addColumn('number', 'Value');
  data.addRows(1);
  data.setValue(0, 0, <?=$phpdb_title?>);
  data.setValue(0, 1, <?=$phpdb_value?>);	
}
   
</script>

Open in new window

0
 
LVL 3

Expert Comment

by:wktang83
ID: 22723371
0
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 22737238
"I need to use the data I got from the DB in javascript function that is stored in another js file."

You need to clarify WHAT other JS file -- where is it at, and what relation to the form passing data to the PHP file?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question