0
$results = db_select('users', 'u') ->fields('u',array('name','uid','mail')) ->execute();
$options = array();
foreach ($results as $result) {
$options[$result->uid] = $result->name.' | '.$result->mail;
}
unset($options[array_search(0, $options)]);
$form['user'] = array(
'#type' => 'select',
'#title' => t('Select User'),
'#options' => $options,
'#default_value'=>('select a value'),
'#states' => array(
'visible' => array(
':input[name="data_set"]' => array('value' => 'education'),
 ),
 ),
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Expire')
);

I have this dropdown , how can I get the default value like Select a value or something

0

For required items a -select- item is added automatically. If the form field is not a required one, you'll have to add it manually, for example with the following code.

  $options = array('_none' => '-select item-') + $options; //Places an extra option at the start

If it's the first item it should be selected automatically but if you want to be safe.

  // Use the array key, not the value.
  '#default_value' => '_none', 

You'll have to filter it out the '_none' value in the submit handler afterwards.

2
  • Sorry @Suranga, I did not get your answer, should I use Soptions=array('_none'=>'select item-') + $options;? or #default_value=>'_none'?
    – nomeer
    May 13 '15 at 3:54
  • You need to use both.
    – apaderno
    May 13 '15 at 5:32

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.