Jump to content

Drop down on click select stay


jpopuk

Recommended Posts

Hi, I have a drop down menu that I need some help with. I am using a template system where I have 1 file which is the same for every page.

 

When I select a link from the drop down and it loads the page the link goes to the "selected" link. Is there a little bit of PHP script that when the page loads of it changes to the relevant link within the drop down?

 

Here is the my drop down code:

 

<form class="form_margin" name="form" id="form">
      <select name="jumpMenu" class="wood" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
                <option value="link1.htm" selected="selected">Link 1</option>
                <option value="link2.htm">Link 2</option>
                <option value="link3.htm">Link 3</option>
</select>
</form>

 

Any help would be great, cheers.

Link to comment
Share on other sites

Hi jpopuk,

 

Using the $_SERVER['PHP_SELF'] variable you should be able to get the current file name and echo "selected" based on that.

 

For example:

 

<form class="form_margin" name="form" id="form">
      <select name="jumpMenu" class="wood" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<?php
$content .= '<option value="link1.php" '.($_SERVER['PHP_SELF'] == "link1.php" ? 'selected="selected"' :'').'>Link1</option>';
$content .= '<option value="link2.php" '.($_SERVER['PHP_SELF'] == "link2.php" ? 'selected="selected"' :'').'>Link2</option>';
$content .= '<option value="link3.php" '.($_SERVER['PHP_SELF'] == "link3.php" ? 'selected="selected"' :'').'>Link3</option>';
echo $content;
?>
</select>
</form>

 

The above uses a Tenary Operator to check the current page name and echo the selected text for the relevant page when found.

 

However, you would need to rename your pages to end in the .php extension for this to work.

 

Hope this helps.

 

 

Link to comment
Share on other sites

If I understand your problem, I had something like this lately, while I didn't find a very efficient solution, the general idea was:

echo "<option value='link1.htm'";if ($_SERVER['PHP_SELF']=="link1.htm") { echo " selected= 'selected'";}echo ">Link 1</option>";
echo "<option value='link2.htm'";if ($_SERVER['PHP_SELF']=="link2.htm") { echo " selected= 'selected'";}echo ">Link 2</option>";
echo "<option value='link3.htm'";if ($_SERVER['PHP_SELF']=="link3.htm") { echo " selected= 'selected'";}echo ">Link 3</option>";

I tried to change it for the purpose I think you had in mind, just from memory, if anything is wrong, it's liable to be the $_SERVER['PHP_SELF'], that was the way I remembered to work out what page you were currently on.

 

Hope this helps

 

~Glenugie~

Link to comment
Share on other sites

Grrrr... they are all great answers, and they all probably work - but I can't seem to get any of them to work, must be something I am doing wrong.

 

Do I need to put a function in, or maybe I am missing something?

 

Cheers,

 

Paul

Link to comment
Share on other sites

Hi Bricktop, I tried your example:

 

<form class="form_margin" name="form" id="form">
      <select name="jumpMenu" class="wood" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
<?php
$content .= '<option value="link1.php" '.($_SERVER['PHP_SELF'] == "link1.php" ? 'selected="selected"' :'').'>Link1</option>';
$content .= '<option value="link2.php" '.($_SERVER['PHP_SELF'] == "link2.php" ? 'selected="selected"' :'').'>Link2</option>';
$content .= '<option value="link3.php" '.($_SERVER['PHP_SELF'] == "link3.php" ? 'selected="selected"' :'').'>Link3</option>';
echo $content;
?>
</select>
</form>

 

And the Dropdown field was empty, it didn't have any errors. - Just a blank dropdown. - Do you know why this could be?

 

Cheers,

 

Paul

Link to comment
Share on other sites

Once you get those pages converted to php, this will be a little more logical and easier to maintain. Put the logic of your code at the top of the page and the output at the bottom of the page. Makes maintenance much easier.

 

<?php
    $pages = array(
        'Page 1' => 'link1.php',
        'Page 2' => 'link2.php',
        'Page 3' => 'link3.php'
    );

    $pageOptions = '';
    foreach ($pages as $link => $page)
    {
        $selected = ($page==$_SERVER['PHP_SELF']) ? ' selected="selected" : '';
        $pageOptions .= "<option value=\"{$page}\"{$selected}>{$link}</option>";
    }
?>



<form class="form_margin" name="form" id="form">
  <select name="jumpMenu" class="wood" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
  <?php echo $pageOptions; ?>
  </select>
</form>

Link to comment
Share on other sites

Cheers Bricktop - thats good to know.

 

mjdamato. - Where the <?php echo $pageOptions; ?> is what goes there, little confused.

 

<form class="form_margin" name="form" id="form">
  <select name="jumpMenu" class="wood" id="jumpMenu" onChange="MM_jumpMenu('parent',this,0)">
  <?php echo $pageOptions; ?>
  </select>

 

Cheers

Link to comment
Share on other sites

mjdamtao - I tried what you said and receieved this error:

 

Parse error: syntax error, unexpected ';' in /home/massimoc/massimoremovals.com/www/templates/layout.tpl.php on line 21

 

this is my line 21:

$selected = ($page==$_SERVER['PHP_SELF']) ? ' selected="selected" : ';

 

Cheers

Link to comment
Share on other sites

In my sig it states

I do not always test the code I provide, so there may be some syntax errors.

 

That line is missing a second single quote mark at the end of the line. That should have been easy to determine.

 

Where the <?php echo $pageOptions; ?> is what goes there, little confused.

 

Not sure what the confusion is.  The other block of code that goes at the top of the page is the "logic" which will build the options you need and assign them to the variable $pageOptions. The code inside the display content (i.e. HTML) simply echos those options to the page.

Link to comment
Share on other sites

Jpopuk, you mentioned that you have one file for every page. If you could post your original code it might be easier for someone to give you a solution that will work with your current code.

 

I am guessing you are using something like $_GET['page'] so that the url is "index.php?page=1." If this is the case, using PHP_SELF doesn't really make sense, but the theory can still be applied by checking the $_GET variable in the same way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.