Solved

pass php variable to js file

Posted on 2008-10-15
4
1,151 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now