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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Box trouble

Status
Not open for further replies.

wraygun

Programmer
Dec 9, 2001
272
US
Hi all,

I've googled until my fingers hurt, searched the threads here, but I'm not finding what I'm looking for.

I have a Select Box populated:
Code:
<select name="selectprimarygenre" id="selectprimarygenre" dir="ltr">
       <option selected>Acoustic</option>
       <option>Alternative</option>
       <option>Ambient</option>
       <option>Beats-N-Loops</option>
        <option>Blues</option>
</select>

I would like to dynamically select one of the items from the list based on data I will collect from another page.

Let's say for instance I've determined that I need 'Ambient' selected when the user gets to the page. I'm also using php, so I can echo the appropriate HTML code if I can just determine what that is. I greatly appreciate any pointers or tips.

Harold

***You can't change your past, but you can change your future***
 
Any pointers and tips would have to come from PHP forum, since HTML is just a markup language and cannot add any logic to the script. You would simply echo [tt]selected="selected"[/tt] next to the option you have determined to be the right one.
 
You say your having the list populated.
If it's populated within a loop, you simply check:

Code:
if ($_GET['selectprimarygenre'] == $row['value']){
    "<option selected=\"selected\">{$row['value']}</option>\n";
  }
else {
    "echo <option>{$row['value']</}</option>\n";
  }

If the list never changes, I would not regenerate it on each load, I would rather do a dirty add at the top:

Code:
<select name="selectprimarygenre" id="selectprimarygenre" dir="ltr">
<?php
if (isset($_GET['selectprimarygenre'])) {
echo "       <option selected=\"selected\">[{$_GET['selectprimarygenre']}]</option>\n";
}
?>
       <option>Acoustic</option>
       <option>Alternative</option>
       <option>Ambient</option>
       <option>Beats-N-Loops</option>
       <option>Blues</option>
</select>

good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
The dirty add was perfect. Thank you so much! This helped tremendously.

Harold

***You can't change your past, but you can change your future***
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top