Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

pass php variable to js file

Posted on 2008-10-15
4
Medium Priority
?
1,160 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

598 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