Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP, Javascript, Session Variables = Head ache

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
I have an issue and need a tissue. =)

I've have this collapsible menu I built with <span>'s. One of the items looks like this:

Code:
<a href="javascript:toggleMenu('menu')" class="menuHead">+ Menu Header</a><br>
        <span id="menu">                                                                                                                                                                          
                <a href="#" class="menuOption">- Option 1</a><br>
		<a href="#" class="menuOption">- Option 2</a><br>
	</span>

The toggleMenu function is just changing the style.display to "blick" to show the span.

I am trying to set a session variable when someone clicks on a menu header. This way when the user clicks on a link under the header, the next page will still have that menu header expanded.

I've tried onClick with PHP setting the session var, but evidently PHP doesn't listen for "onClick" and it just does it every time, thus expanding every header every time.

I'm looking for suggestions on how to accomplish this.

KizMar
------------
 
PHP = server side
JavaScript = client side

PHP does not "listen" to anything. It's like a phone call: The PHP is called, it delivers the content and hangs up.

You'll need more tissue.
 
So how can I accomplish this then?

I can't set the server variable when someone clicks on a link and read it again on the next page when it loads?

KizMar
------------
 
Suggestion:
1. You can write client side code that sets a hidden field to a specific value in the browser when a menu item is selected.
2. CLient side code is triggered by the link click and submits the form with the hidden field and the info where to go next.
3. PHP receives the info and then it is able to set a session variable.

Caveat:
Non JavaScript browsers cannot navigate.
 
OK, I think I'm getting closer here... I'm using a form with a "get" method, but on the next page where the url has my "...?var=menu01", I still am unable to retrieve the value of "var" with javascript.

KizMar
------------
 
Have PHP put that into the output. Print or echo the appropriate JavaScript code that sets the variable to the passed value.
Or you can do URL dissection:
Code:
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("&") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}
However, here we are again leaving the realm of PHP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top