Javascript + Dynamic php data

I have a menu done with javascript. Lets say it is just like this:

<javascript>
code
<menu item01>
<submenu item01>
<submenu item02>
<submenu item03>
<menu item02>
<submenu item01>
<menu item03>
<submenu item01>
<submenu item02>
<menu item04>
</script>

What Id like to do is to get data from a database using php (i know how to do this) and load it where I have my menu itens.
For example:

$sql = "select * from db";
rest of the code

then javascript
<javascript>
code

and then php inside it:
for(i=0;i<tot;i++){
menu item[i]
etc
}

Is that possible ??
How can I do that?
[808 byte] By [rogernem] at [2007-11-19 22:57:32]
# 1 Re: Javascript + Dynamic php data
A possible solution would be to use ajax.

With ajax you can make another http request from the client to a certain script on the server. than the server could make the sql query and returns the data to the js script, which has to handle it.

have a look into the ajax forum!

here are some tutorials on ajax:

http://developer.mozilla.org/en/docs/AJAX:Getting_Started
http://rajshekhar.net/blog/archives/85-Rasmus-30-second-AJAX-Tutorial.html
bigBA at 2007-11-8 0:40:18 >
# 2 Re: Javascript + Dynamic php data
AJAX would work, but it is not fully browser compatible yet.

I did a number that can do what you want.

index.html
<script language="JavaScript">
function updatediv(){
theframe.location.replace('update.php');
}
</script>

<iframe name="theframe" src="about:blank" width=0 height=0 frameborder=0></iframe>

<div id="divupdate">
</div>

update.php
<?php
$connection = mysql_connect('localhost', 'user', 'password');
$db = mysql_select_db('database');

$fill = "<select id='selecta' name='selecta'>";
$sql = mysql_query("SELECT * FROM `database`");
while($info = mysql_fetch_assoc($sql)){
$value = $info['value'];
$text = $info['text'];
$fill .= "<option value='" . $value . "'>" . $text . "</option>"
}
$fill .= "</select>"

mysql_close($connection);

echo "
<script language='JavaScript'>
parent.document.getElementById('divupdate').innerHTML = '$fill';
location.replace('about:blank');
</script>
";
?>
PeejAvery at 2007-11-8 0:41:21 >
# 3 Re: Javascript + Dynamic php data
thanks. thats very nice
ill try that.
rogernem at 2007-11-8 0:42:22 >