Page 1 of 1

php and javascript

Posted: Wed May 17, 2006 3:05 pm
by Dedix
I am trying to make a javasript dropdown menu with php variables. I have no clue how to pass variables to this menu from an text database file via php. I have a external js file.

The links will look like this:

link1 --> example.com/index.php?link_id=1
link2 --> example.com/index.php?link_id=2

etc. until link ids are available in the database file

Any help would be appreciated.

Posted: Wed May 17, 2006 3:07 pm
by Burrito
assuming you're getting the links from a db I am?

echo out the results you should:

ex:

Code: Select all

$result = mysql_query("select * from....");
while($row = mysql_fetch_assoc($result))
{
    echo $row['link'];
}

Posted: Wed May 17, 2006 3:22 pm
by Dedix
No, my problem is I don't know how to pass php variables into the js file.

here is the some line of the menu script file:

Code: Select all

startMainMenu("",0,0,2,0,0)
mainMenuItem("elements_page1_hnavbar_b1",".png",27,144,"javascript:;","","home",2,2,"elements_page1_hnavbar_plain");
mainMenuItem("elements_page1_hnavbar_b2",".png",27,144,"javascript:;","","about us",2,2,"elements_page1_hnavbar_plain");
mainMenuItem("elements_page1_hnavbar_b3",".png",27,144,"javascript:;","","services",2,2,"elements_page1_hnavbar_plain");
mainMenuItem("elements_page1_hnavbar_b4",".png",27,144,"javascript:;","","news / special offers",2,2,"elements_page1_hnavbar_plain");
endMainMenu("",0,0);

startSubmenu("elements_page1_hnavbar_b3","elements_page1_hnavbar_menu",144);
submenuItem("SubMenuText 1","javascript:;","","elements_page1_hnavbar_plain");
submenuItem("SubMenuText 2","javascript:;","","elements_page1_hnavbar_plain");
submenuItem("SubMenuText 3","javascript:;","","elements_page1_hnavbar_plain");
endSubmenu("elements_page1_hnavbar_b3");
Instead of "SubMenuText 1..." I want the php generated variables. It is more difficult to me cause the numbers of submenu is always changing depending on how many links are in the db file.

Posted: Wed May 17, 2006 3:28 pm
by Burrito

Code: Select all

startSubmenu("elements_page1_hnavbar_b3","elements_page1_hnavbar_menu",144); 
<?
while($row = ...)
{
?>
submenuItem("<?=$row['linktext'];?>","javascript:;","","elements_page1_hnavbar_plain"); 
<?
}
?>
endSubmenu("elements_page1_hnavbar_b3");

Posted: Wed May 17, 2006 3:51 pm
by Dedix
Burrito wrote:

Code: Select all

startSubmenu("elements_page1_hnavbar_b3","elements_page1_hnavbar_menu",144); 
<?
while($row = ...)
{
?>
submenuItem("<?=$row['linktext'];?>","javascript:;","","elements_page1_hnavbar_plain"); 
<?
}
?>
endSubmenu("elements_page1_hnavbar_b3");
Sorry I don't get you. I quoted from js file not from php. It is an external js file that page calls.

Posted: Wed May 17, 2006 3:54 pm
by Burrito
have to use a php file you probably will.

use header() you should to send the file as text/javascript you must.

point to the .php file in your js src attribute you will.

ex:

Code: Select all

<script src="somefile.php"></script>

Posted: Wed May 17, 2006 4:15 pm
by Dedix
Umm, I am not a php or js guru...

Code: Select all

$file=file($services_db);
$s=sizeof($file);
for($i=0;$i<$s;$i++){
$ma=explode("|",$file[$i]);

$menuitems.='&nbsp;- <a href ="index.php?mode=services&serviceID='.$ma[0].'">'.$ma[1].'</a><br>';
	}
Above is my php code that generates links and i would like to use it with that js code i have quoted. So I want the $ma[1] variable instead of SubMenuText 1-2-3... and so on.

Posted: Wed May 17, 2006 4:29 pm
by Burrito
a good resource for you I have found:

http://www.javascriptkit.com/javatutors ... lphp.shtml

Posted: Wed May 17, 2006 4:58 pm
by Dedix
Burrito wrote:a good resource for you I have found:

http://www.javascriptkit.com/javatutors ... lphp.shtml
Thank you Burrito, I am gonna check it out.

Posted: Thu May 18, 2006 8:52 pm
by Dedix
My problem is resolved, no more replies.
Thanks.