Solved

pass php variable to js file

Posted on 2008-10-15
4
1,154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

739 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