I have a drop-down navigation that brings the user to the page selected in the drop-down once the user clicks a button. I have a function that takes in the page section title as a parameter and builds a new drop-down with that page on top of the list, so that each page you navigate to will remain in focus in the drop-down. The function is a massive switch case with a lot of repetitive code and I know there's an easier and cleaner way of doing this... I'm just having trouble thinking of a solution. Here's a code snippet, I won't post the entire switch case:
function makeDropDown($page){
switch ($page){
case 'uploads':
echo '<center><form action="pictures.php" action="get">
<select name="pictype">
<option value="uploads">Uploads</option>
<option value="fromweb">From Web</option>
<option value="wallpapers">Wallpapers</option>
<option value="misc">Miscellaneous</option>
</select>
<input type="submit" value = "Go!"/>
</form></center>'; break;
case 'fromweb':
echo '<center><form action="pictures.php" action="get">
<select name="pictype">
<option value="fromweb">From Web</option>
<option value="uploads">Uploads</option>
<option value="wallpapers">Wallpapers</option>
<option value="misc">Miscellaneous</option>
</select>
<input type="submit" value = "Go!"/>
</form></center>'; break;
case 'wallpapers':
echo '<center><form action="pictures.php" action="get">
<select name="pictype">
<option value="wallpapers">Wallpapers</option>
<option value="uploads">Uploads</option>
<option value="fromweb">From Web</option>
<option value="misc">Miscellaneous</option>
</select>
<input type="submit" value = "Go!"/>
</form></center>'; break;
Any suggestions?
Instead of moving the selected item to the top of the list, why not try using the selected
attribute of the option items. This will initialize the select box to the selected option. simply echo selected
in the option that matches the value of $page
function makeDropDown($page){
echo '<center><form action="pictures.php" action="get">';
echo ' <select name="pictype">';
echo ' <option value="uploads"'.(($page=="uploads")? " selected":"").'>Uploads</option>';
echo ' <option value="fromweb"'.(($page=="fromweb")? " selected":"").'>From Web</option>';
echo ' <option value="wallpapers"'.(($page=="wallpapers")? " selected":"").'>Wallpapers</option>';
echo ' <option value="misc"'.(($page=="misc")? " selected":"").'>Miscellaneous</option>';
echo ' </select>';
echo ' <input type="submit" value = "Go!"/>';
echo '</form></center>';
}
This way, if $page=='misc'
, your HTML will result this way
<center><form action="pictures.php" action="get">
<select name="pictype">
<option value="uploads">Uploads</option>
<option value="fromweb">From Web</option>
<option value="wallpapers">Wallpapers</option>
<option value="misc" selected>Miscellaneous</option>
</select>
<input type="submit" value = "Go!"/>
</form></center>'
You could try echo-ing this at the end beginning of each case:
<script type="text/javascript">document.getElementsByName(name).focus();</script>
Hope that helps.