Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1127 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Adding "selected" into a select form

Thread title: Adding "selected" into a select form
Reply    
    Thread tools Search this thread Display Modes  
06-27-2012, 01:34 AM
#1
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old  Adding "selected" into a select form

I have $country which stores the 2 letter country code of a user. US for example.

Code:
<select id="countries">						
    <option value="US">United States</option>
    <option value="EN">England</option>
    <option value="AU">Australia</option>
    <option value="CA">Canada</option>
    etc.
</select>
What's the best practice to add "selected" to the option of the users country code?

I'm against adding a million if's.

Reply With Quote
06-27-2012, 03:44 AM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

I don't see a good way to automate a static list. Since the content doesn't seem to have much variance have you considered keeping a list of country codes in a config file or something? That way you could output it all in a loop. This would also allow you to easily add more at a later time if you need to.

06-27-2012, 03:58 AM
#3
Dan is offline Dan
Dan's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 3,164
iTrader: 15 / 86%
 

Dan is an unknown quantity at this point

  Old

I cheated and used jQuery.

$('#countries').val('<?=$country?>');

Simple.

Reply With Quote
Reply    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed