Forum Moderators: coopster

Message Too Old, No Replies

Merging PHP & Javascript

Need to php execute code when 'onclick'

         

PumpkinHead

11:17 am on Aug 26, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm not sure if this is possible but I have a folding tree menu that was created in javascript. When you click a menu item at the highest level, the menu expands and the items at the next level are shown.

E.g

News
----
(When "News" clicked...)Today's News
(When "News" clicked...)Yesterday's News
(When "News" clicked...)News Archive

The problem I have is that when a link is clicked (E.g Yesterday's News) the folding tree menu is redisplayed and all menus collapse as the script does not hold details of what was clicked.

I'm trying to add PHP sessions to this script to resolve this.


<p class="nav_menu_head">
<a href="#" onclick="WM_toggle('toggle_0001'); return false<?php if ($nav_01 == 'CLOSE') {$_SESSION['nav_01'] = 'OPEN'; $nav_01 = 'OPEN';} else {$_SESSION['nav_01'] = 'CLOSE'; $nav_01 = 'CLOSE';}?>">Today's News</a>
</p>

In the above code, the php (to toggle between on and off) is always executed, even if the menu item is not clicked.

Can anyone tell me if it's possible to merge this code?

Cheers in advance all

zulu_dude

11:50 am on Aug 26, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



Part of the problem is that PHP is server-side and javascript is client side... therefore the only way for them to interact (AFAIK) is through POST or GET variables.

In other words, the PHP code that you have in the onclick event is executed before the user even clicks on the menu, as it is run when loading the page. Which would explain why you're getting the problem of it always toggling.

It's difficult to offer much advice without seeing the javascript, but I presume there must be some sort of variable set in the javascript to determine whether a menu is expanded or not?

If so, the best way forward that I can see would involve using a variable in the URL. In other words, use this url for your links:

<a href="www.yoursite.com/latest_news.php?menuid=2">Latest News</a>
<a href="www.yoursite.com/todays_news.php?menuid=2">Todays News</a>
<a href="www.yoursite.com/yesterdays_news.php?menuid=2">Yesterdays News</a>

Then, when loading your page, you can use the php $_GET['menuid'] variable to make sure that the menu branch corresponding to 2 is expanded. In other words, do some sort of conditional statement on menuid and echo the correct variable (e.g. 'expanded', 'not_expanded') to the corresponding menu branch in the javascript.

Hope this all makes sense!

zulu_dude

11:53 am on Aug 26, 2005 (gmt 0)

10+ Year Member Top Contributors Of The Month



On second thoughts, you could also just edit the javascript menu according to which page it's on. In other words, hard-code it so that the 'News' menu is always expanded on the Todays news, latest news and yesterdays news pages.

It would probably be easier to implement initially but much harder to change site-wide at a later stage.