Jump to content

Change selected list drop down value...


lidds

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
Link to comment
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]
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.