Reputation: 1619
I have this drop-down list of countries:
<select name="country">
<option value="">Country...</option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<!-- Removed several options as they are not needed for question -->
<option value="Zaire">Zaire</option>
<option value="Zambia">Zambia</option>
<option value="Zimbabwe">Zimbabwe</option>
</select>
When a user edits their profile, I want the country that the user selected (in the database) to be selected by default.
When the user registered, Egypt
was selected. When the user edits their profile, I want this drop-down list to select Egypt
by default (until changed).
Upvotes: 0
Views: 1045
Reputation: 18795
Simply add the selected
attribute to the option
the user chose when creating an account.
<?php
$country_list = '<select name="country">';
$selected_country = $user_info['country'];
foreach($countries as $country){
$is_selected = ($country===$selected_country);
$country_list .= '<option value="'.$country.'"'.($is_selected ? ' selected' : '').'>$country</option>';
}
$country_list .= '</select>';
<?php
...
$country_list = '<select name="country">';
$selected_country = $user_info['country'];
$stmt->bind_result($country);
while($mysqli->fetch()){
$is_selected = ($country===$selected_country);
$country_list .= '<option value="'.$country.'"'.($is_selected ? ' selected' : '').'>$country</option>';
}
$country_list .= '</select>';
Note: While it will use up slightly more resources, I would suggest using an array of countries at the very least, but preferably store the countries in the database. The reason for the database being if you ever change the name of a country (e.g., USSR to Russia), you won't have to update it for each user as well (since they'd be linked to the id
in the countries
table). I'd suggest database or array due to the fact that sooner or later you'll probably change the markup of the option
list at some point in time, and having the list in an array will make it possible to change a single line, rather than once for every country listed.
Upvotes: 1
Reputation: 22241
Define a variable from the database value.
Store the entire <select>
tag as a variable.
Find and replace the selected option value with the selected attribute.
$country_selected_value;
// defined from database
$country_select = '<select name="country">...</select>';
// ... represents your options
$value_search = 'value="'.country_selected_value.'"';
// what to look for in the select
echo str_replace($value_search , $value_search.'" selected="selected"', $country_select);
// displaying the select field with the selected option
If you need help getting data from the database there are many such topics already on SO.
Upvotes: 0
Reputation: 1569
Html syntax is this:
<option value="AAAA" selected>label</option>
So in PHP, load from DB your values, and your selected value, when you print them in a loop, if current printing one is the selected one just echo a "selected".
Upvotes: 0