After beating my head against the desk the past hour, I finally figured out that to set a default value on a plain select list, you need to use "#value" and not "#default_value". So, for example, say I have an array of banks consisting of the bank id and the bank name, like so:
$banks = array(14=>"bank one", 22=>"bank two); |
I then want to construct a simple select form element, this WILL NOT work:
$form['drop'] = array( #id' => "bank_drop", '#type' => 'select', '#title' => 'test', '#options' => $banks, '<span style="color: #ff0000;">#default_value</span>' => 22 // will NOT work! ); |
But this works:
$form['drop'] = array( #id' => "bank_drop", '#type' => 'select', '#title' => 'test', '#options' => $banks, '<span style="color: #339966;"><strong>#value</strong></span>' => 22 // works! ); |
This was on Drupal 6.15
Hope that saves someone some headache :)


By Richard McIntyre April 7, 2010 - 11:53 pm
You saved me a huge head-ache, thank you!
By Paige Cook May 13, 2010 - 8:03 pm
Thanks, I was beating my head on this one as well. But thankfully it was not for long, thanks to this post :-)
By Maduka May 17, 2010 - 7:53 am
You save my time too. thank you very much dude :)
By ethan June 10, 2010 - 11:43 am
Well, this is nice. But then if a user changes the SELECT box, the new value is ignored on submit.
By Matt June 10, 2010 - 12:22 pm
Don’t know whatcha doin’ there ethan, but all the above is doing is spitting out an html form with an option preselected. Sounds like something else is happening post-submit.
By Shannon July 7, 2010 - 12:32 am
Thanks! This has been the answer to my issue for the last couple of hours… Drupal’s Form API is misleading on this front. It feels like a bug…
By Jim April 15, 2011 - 5:41 am
Wow great answer and yes it saves me a headache and boy was it simple.
Thanks for your help and advice , Jim
By betoss April 27, 2011 - 8:09 am
thanks! has served me well
By Paul Bennett May 3, 2011 - 7:52 pm
Thanks for posting! This would have taken me ages to track down :)
By Martin January 9, 2012 - 1:41 pm
In Drupal 7, you should use #default_value, because:
“Description: The value of the form element that will be displayed or selected initially if the form has not been submitted yet. Should NOT be confused with #value, which is a hard-coded value the user cannot change! ”
(http://api.drupal.org/api/drupal/developer–topics–forms_api_reference.html/7#default_value)
By Nilamkumar Patel April 9, 2012 - 11:29 am
Thank you very much. You saved my time and provided very helpful information.
By Drupal select list default value form | March 21, 2013 - 10:28 am
[...] http://www.givegoodweb.com/post/132/drupal-select-list-default_value [...]