[web] Sending php varibles to a JS function in an onload call

Started by
2 comments, last by Verminox 17 years, 6 months ago

<?php
mySQLConnect stuff...
$vals = mysql_num_rows($result1);
?>

<body onload="demo('a', 'b', 'c');">..
</body>
I have a JS class that draws a graph and gets called by the demo() function. However, what I'm graphing is data I pull out of a mySQL db. So I get the data with php, but I don't know how to send it to the JS call so that I can graph it. Ex: so instead of demo(a, b, c), I need demo(a, b, $vals).
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Advertisement
Using &#106avascript for this seems like unnecessary work. Will it do anything that a static graph generated by PHP wouldn't do (either in HTML or as an image)?
Not really; I was using this library:
http://webfx.eae.net/dhtml/chart/api.html
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
If you want this to be generated dynamically, but once every page refresh only, then just use:

<?php/* mySQLConnect stuff... */$vals = mysql_num_rows($result1);?><html><body onload="demo('a','b','<?php echo $vals; ?>');"></body>


If you have a PHP array with you that you want to give to a function as args, simple use explode() with a comma separator.



On the other hand, if your graph or whatever it is, is going to change dynamically and on-the-fly (multiple times every page refresh) then you may want to look into some basic AJAX techniques like XMLHttpRequest() to talk to the server while a page is being viewed idly.
--------------------------------------Amaze your friends! Astound your family! Kennify your text!

This topic is closed to new replies.

Advertisement