Solved

pass php variable to js file

Posted on 2008-10-15
4
1,149 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
Comment Utility
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
Comment Utility
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
"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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This article discusses how to create an extensible mechanism for linked drop downs.
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 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…

763 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

11 Experts available now in Live!

Get 1:1 Help Now