views:

36

answers:

2

I want to append: selected="selected" to an option in my dropdown if my session value is equal to the value of the option.

Example:

   if($_SESSION['sms-timezone'] == 'Africa/Abidjan') 
append selected="selected" where option value="Africa/Abidjan"

<select>
<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>
<option value="Africa/Asmera"> Africa/Asmera( +03:00 GMT ) </option>
  <option value="Africa/Bamako"> Africa/Bamako( +00:00 GMT ) </option>
  <option value="Africa/Bangui"> Africa/Bangui( +01:00 GMT ) </option>
  <option value="Africa/Banjul"> Africa/Banjul( +00:00 GMT ) </option>
  <option value="Africa/Bissau"> Africa/Bissau( +00:00 GMT ) </option>

The reason for appending to the option is because the dropdown contains 500+ items.

A: 
<option value="Africa/Bamako" <? if($_SESSION['sms-timezone'] == 'Africa/Bamako') { echo 'selected="selected"';} ?> > Africa/Bamako( +00:00 GMT ) </option>
Evernoob
A: 

If your options are generated programmatically in a loop it would be fairly easy to put a check in each loop to set the "selected" on the right element.

If you just have each option element written out statically, then you would need to put an if statement inline with each option tag to check if the one in session matches the current option. Don't do this please.

Lastly, you could use JS. Something like this would be pretty trivial in something like JQuery.

threendib