0

I have a form with a select drop-down for country codes, and with option text that displays the full country name, like so:

<select name="country_code">
  <option value="AUS">Australia</option>
  <option value="CAN">Canada</option>
  <option value="GBR">United Kingdom</option>
</select>

After a user has chosen their country and stored the country_code data into an expressionengine field, how can I pre-select their country code and associate the full country name afterwards when they want to edit their country in the future? For example, something like this:

<select name="country_code">
  <option selected value="{country_code}"><!-- full country name here --></option>
  <option value="AUS">Australia</option>
  <option value="CAN">Canada</option>
  <option value="GBR">United Kingdom</option>
</select>
3
  • 1
    Why not save your user the trouble and list all countries? You could use an 3rd party plugin: Reegion-Select, devot-ee.com/add-ons/reegion-select – Nutmeg Jul 23 '14 at 8:48
  • Thank you for the suggestion, that plugin/fieldtype looks like it might work for this. I do want to customize the country list, however. In total there will be around 25 countries which will be selectable. I'll give that plugin a shot and will report back. – positlux Jul 23 '14 at 13:13
  • @Nutmeg: the Reegion-Select is working quite nicely, and I've found it is quite flexible. Please go ahead and make it an answer and I'll accept it. Also, if Derek Hogue should happen to come across this question, many thanks for developing the plugin and making it freely available. – positlux Jul 26 '14 at 3:35
1

Why not save your user the trouble and list all countries? You could use an 3rd party plugin: Reegion-Select, devot-ee.com/add-ons/reegion-select.

2

I'm assuming you have "AUS" saved in the members profile, you could use this conditional:

<select name="country_code">
  <option value="AUS"{if country_code == "AUS"} selected{/if}>Australia</option>
  <option value="CAN"{if country_code == "CAN"} selected{/if}>Canada</option>
  <option value="GBR"{if country_code == "GBR"} selected{/if}>United Kingdom</option>
</select>

Disclaimer: I'm basing this on your example where you only have a few countries!!

2
  • I thought about this solution, but unfortunately my example was just a short sample and I do indeed need to have this for about 25 different countries. I appreciate it though, and it might be able to help someone else with a shorter list of options. – positlux Jul 23 '14 at 13:10
  • 1
    In that case the country plugin will work as the values are stored as value:option pairs negating the need to test for just the value. – stuartmcd69 Jul 23 '14 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.