I sometimes find that I need to use PHP to generate a JavaScript array that is used by an application. In this situation, I am probably using data in a MySQL database to generate the array. This is no different than when you generate any other client-side code.
For example, if you had a MySQL table loaded with names of books that you want to use to generate a JavaScript array, you would do something like this:
<?php
echo "var bNames = new Array(";
$sql = "SELECT book_name FROM books ORDER by book_name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "\"".$row['book_name']."\",";
}
echo "\"\"";
echo ");\n";
?>
This generates the following client-side JavaScript array:
var bNames = new Array("book name 1","book name 2","book name 3","");
Before you jump into using JavaScript heavily for Web applications, keep in mind that JavaScript should never be used for a critical function. According to the W3Schools surveys, up to 10% of users have JavaScript disabled in their browsers. I think this is mostly due to people who set the security level to high in Internet Explorer, which disables all active scripting, including JavaScript.