Topic: Using the results from a MySQL select (php) in my Javascript program.

I want to use the results from a SELECT on my database in my Javascript code. I realize I am trying to transcend the Client / Server domain exclusivity thing. At the moment, I succeed in fetching my data and echoing it (server side). This is OK. NOW, is there a trick to getting my record data into my calling JAVASCRIPT code so I can do some work with it using Javascript?

Thanks very much.

Vote up Vote down

Re: Using the results from a MySQL select (php) in my Javascript program.

I would probably look into some AJAX for this sort of thing. I personally would suggest looking at jQuery's ajax functionality (here's a basic screencast, I'm sure you can find others too with a web search or two: http://blog.themeforest.net/tutorials/j … s-day-13/) I'm sure it can also be done with other methods, using a different Javascript framework, or no framework at all, but I suggest jQuery for the ease of use.

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Using the results from a MySQL select (php) in my Javascript program.

Also, Stefan recently put out a course on jQuery that includes some videos on working with PHP and AJAX. You do have to pay for the course, whereas the other link is free, but it may be worth a look.

http://www.killerjavascript.com/beginners-jquery/

Benjamin Falk | Falken Creative : Twitter
Skills: Photoshop, Illustrator, HTML, CSS, jQuery, PHP and CodeIgniter

Vote up Vote down

Re: Using the results from a MySQL select (php) in my Javascript program.

If you can echo the data from the database, then just echo it into a <script> tag.

You may need to code it like:

<script type="text/javascript">
var myData = new Array();

<?php
$x = 0;
$js = '';

//php loop through your dataset
foreach($rows as $row){
    $js .= "myData[".$x."] = '".$row->id."';";
    $x = $x++;
}

echo $js;
?>

//
//now you can use Javascript on the array myData
//
</script>

just to give you an idea.

Last edited by BeeDev (September 16, 2009 3:59 am)

Vote up Vote down