I have been searching for hours and can't seem to locate information on something that I feel should be pretty simple.

I want to click on a hyperlink and set a session variable.
I've found examples on how to pass variables between pages but not within the same page.

I would prefer not to pass the variables via the URL because I want to keep the URL clean. I also gather that it is better coding practice to use session variables.

I dont want to pass the variables using a form whereby the user populates a textbox. Instead, I want the variables embedded within the url.

Here's an example of what I'm trying to achieve:

##########################
<?php
session_start();
?>

<body id="twoColLayout" class="<?php echo $_SESSION['submenu']; ?>">

<form action="index.php" method="POST">
<a href="index.php?submenu="search_bookmark">Search bookmarks</a>
</form>
##########################

Objective - When I select the link, the page reload with the variable 'submenu' set to 'search_bookmark'.
This then sets the class within the <body> tag. This is used for a CSS style sheet.

Can anyone please provide a helping hand. Or alternatively suggest a better way of doing this. To be honest I'm a PHP beginner.

thanks and regards.

    thanks scrupul0us.

    whilst that may work I'm very interested on how to do it with PHP using session variables(from a learning perspective).

      You are on the right way. However, a link is not part of form data sent as post, and besides, you'd need a submit button (or javascript) to submit the form. So, your link would still end up changing the url.

      $ok_vals = array('menu1', 'menu2');
      if (isset($_GET['submenu']) && in_array($_GET['submenu'], $ok_vals))
      	$_SESSION['submenu'] = $_GET['submenu'];
      else if (!isset($_SESSION['submenu']))
      	$_SESSION['submenu'] = 'default';

      To deal with this without mucking up your url, switch $_GET in the above code and instead use

      <form action="index.php" method="post">
      	<select name="submenu">
      		<option value="menu1">Menu 1</option>
      		<option value="menu2">Menu 2</option>
      	</select>
      	<input type="submit" value="Submit" name="submit"/>
      </form>
      

        thanks for your time johanafm.

        I've tried your suggestion but the section of code that outputs the user selection (below) keeps printing 'default' which is the 'else if' condition.

        <body id="twoColLayout" class="<?php echo $_SESSION['submenu']; ?>">

        Whilst I would like to fix the above I'm thinking that if I need a drop down box which displays the selected item then there is no point highlighting the current navigation (which is why I use a class within body). However, the drop down box always defaults to 'Menu 1' after I click submit on 'Menu 2'.
        Is there a way to fix this?

        thanks again for your advice.

          johanafm;10936738 wrote:

          To deal with this without mucking up your url, switch $_GET in the above code

          That is, every $GET should be $POST instead. $_GET would be to use links. Or of course if you sepcify method="get" for the form.

            Write a Reply...