Jump to content

Archived

This topic is now archived and is closed to further replies.

lidds

Change selected list drop down value...

Recommended Posts

I have been searching google to try and find the answer but have not found the solution.

What I want to do is change the select value in a html list drop down depending on a value from a DB query. For example I have a drop down that has a list of countries (manually entered, not from DB) on a edit user details page, so if the user has already registered as country UK, I want this to be the one that is selected.

I hope I explained that OK.

Thank you in advance

Simon

Share this post


Link to post
Share on other sites
Your code/life will be simplest if you create an array of all the countries, and then use a simple piece of code like the below to generate your drop-down and show the selected country:
[code]include("country_array.php");
echo "<select size='1' name='country'>";
for ($i=0;$i<count($countries);$i++ {
    echo "<option value='". $countries[$i]. "'";
    if ($country_from_db==$countries[$i]) { echo " selected style='background-color:#6ff'"; }
    echo ">". $countries[$i]. "</option>\n";
}
echo "</select>";[/code]
The countries array file looks like:
[code]
$countries = array("Afghanistan", "Albania", lots more sountries, "Zambia", "Zimbabwe");[/code]

Share this post


Link to post
Share on other sites

×

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.