Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

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

This topic is 4365 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Using javascript 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)?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!