Sign in to follow this  
Sfpiano

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

Recommended Posts

<?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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this