php and javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

php and javascript

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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'];
}
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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");
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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>
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

a good resource for you I have found:

http://www.javascriptkit.com/javatutors ... lphp.shtml
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

Post 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.
Dedix
Forum Newbie
Posts: 8
Joined: Mon Feb 24, 2003 12:13 pm

Post by Dedix »

My problem is resolved, no more replies.
Thanks.
Post Reply