jpopuk Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/ Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933028 Share on other sites More sharing options...
micah1701 Posted October 8, 2009 Share Posted October 8, 2009 something like: <option value="link1.htm" <?php if(preg_match("link1.htm",$_SERVER['PHP_SELF'])){ echo "selected=\"selected\" "; } ?>>Link 1</option> Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933029 Share on other sites More sharing options...
Glenugie Posted October 8, 2009 Share Posted October 8, 2009 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~ Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933030 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933068 Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi jpopuk, Post your code and any error(s) you're receiving and we'll take a look. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933070 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933080 Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi Paul, Did you rename the page you're using this code on to a .php extension? i.e. instead of index.html change to index.php? Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933083 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 Does every page this script works on have to be .php extension? Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933088 Share on other sites More sharing options...
Bricktop Posted October 8, 2009 Share Posted October 8, 2009 Hi Paul, Yes, everytime you use PHP code on one of your pages, you must rename that page to a .php extension for the server to parse it as PHP code. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933092 Share on other sites More sharing options...
Psycho Posted October 8, 2009 Share Posted October 8, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933112 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933114 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933115 Share on other sites More sharing options...
Psycho Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933141 Share on other sites More sharing options...
jpopuk Posted October 8, 2009 Author Share Posted October 8, 2009 Nope, still no joy. Thanks any way for the help. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933159 Share on other sites More sharing options...
lemmin Posted October 8, 2009 Share Posted October 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176959-drop-down-on-click-select-stay/#findComment-933166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.