Reputation: 91
Apologies if a similar question has been asked in the past - I did search the previous questions but had no luck finding the answer I needed.
I currently have an account option on my website with a 'my settings' area, that allows users to change their details. One of the details they can change is their country. Instead of allowing the user to type the country (in a html textbox), I've made things more restricted by providing a dropdown list of most countries in the world. I've briefed my code below (as I don't want to paste a list of countries and waste time):
<select name="country" id="country" class="required" value="<? echo $row_settings['country']; ?>">
<option value=""></option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
...
<option value="UK">UK</option>
<option value="USA">USA</option>
...
</select>
As this 'my settings' area is for registered users, I obviously have a database table - lets call it 'users' and I'll use a simplified version of it, where I have a 'username' column and a 'country' column. The first row of data (i.e. the first user's details) are 'user1' (the username) and 'UK' (the country). As you'll notice, 'user1' lives in the 'UK' - where this data is the same as one of the dropdown list options (i.e. the <option value="UK">UK</option>
shown above).
When on the 'my settings' page, I have selected the option 'UK' from the dropdown list, and clicked the 'Save' button (to UPDATE the data for 'user1' in the table) - however, once the page displays after clicking the 'Save' button, the selected value is, as you'll see above, the first option value in the dropdown list (i.e. <option value=""></option>
). But the thing is - the data in the table is updated successfully - so if I were to select the 'USA' option on the list, it would successfully update the 'country' data for 'user1' to 'USA'.
What I'm wanting is, simply for the user to see the country they selected on the list before clicking the 'Save' button - is there a reason why the first option of the dropdown list displays after the form has been submitted and the page reloads and displays? why is this happening? Shouldn't the table data for the 'country' column display (i.e. the 'UK' option, as I selected this in the dropdown list before submitting the form). Yet if I were to use a html textbox for the country part of the form, it would simply display the table data for the 'country' column for the 'user1' row - i.e. it would display 'UK'.
Sorry if any part of this question is confusing or over-described, and many thanks for any replies. If you need, I will provide a link to the page with temporary login details so you can see what I'm talking about.
Upvotes: 0
Views: 3729
Reputation: 1573
here's a quick POC for you:
<?php
$countries = ['Afghanistan', 'Albania', 'UK', 'USA'];
$row_settings = [
'username' => 'Joe',
'country' => 'UK'
]
?>
<select name="country" type="text" id="country" class="required">
<?php foreach($countries as $country): ?>
<option <?php echo ($row_settings['country'] == $country) ? 'selected' : ''; ?>><?php echo $country; ?></option>
<?php endforeach; ?>
</select>
Upvotes: 1
Reputation: 219077
This doesn't do what you think it does:
value="<? echo $row_settings['country']; ?>"
You don't want to try to set a "value" on the select
, instead you want to set the selected
attribute on the relevant option
element. If, for example, you create those in a loop then your loop might look something like this (pseudo-code):
for (each item)
selected = ''
if (item == user.country)
selected = 'selected'
echo '<option value="' . item . '" ' . selected . '>' . item . '</option'
Upvotes: 0
Reputation: 427
You need to add the selected
tag to the country that you want selected by default, instead of the first in the list.
<option value="UK" selected>UK</option>
So you'd need to read this information from the database before generating the page in order to set-up the drop down properly.
Upvotes: 0