Specific value for option in select

sumgai - May 31, 2006 - 20:39

I'd like to create a form element that is a 'select', but that has a 'value' that isn't the same as the option's display string and also to be able to set the "selected" option.

So, rather than just:

<option>red</option>

I'd like to have elements like:

<option value='red' selected='selected'>The Color Red</option>

I've searched through the form module code, but don't see anything at all about it.

Any help would be mucho appreciado!

Mitch

#options value

nevets - May 31, 2006 - 21:21

When you setup a select field one of the values is '#options' which is set to an array of values to select from.
The format for this array is

$options = array('return value 1' => t('Display value 1'), 'return value 2' => t('Display value 2'));

or using your example
$options = array('red' => t(The Color Red'), ...);

Thanks and how about profiles?

sumgai - May 31, 2006 - 21:51

Is there also a way to do this in the profile module? It isn't obvious what code I would insert in the textbod that the profile module provides for entering the options for a select.

Thank you again!

Mitch

For profiles, no way that I know of

nevets - May 31, 2006 - 21:58

I believe for the profile module the displayed and stored value are always the same.

One last question...

sumgai - May 31, 2006 - 22:29

Is there a way to specify 'selected' for one of the options in the select?

You've been a great help! Many Thanks!

Mitch

Set #default_value

nevets - May 31, 2006 - 23:13

If you set #default_value for the select list it will be used as the initial/existing value.

Thank You! Thank You!

sumgai - June 1, 2006 - 13:13

You've been a tremendous help!

hook_elements

_-dave-_ - June 3, 2006 - 08:11

Hi sumgai

You could use hook_elements to add your own fields to the profile module although change the existing one would involve chaning the code...

http://api.drupal.org/api/HEAD/function/hook_elements

Dave

Use code like the following

9802008 - September 12, 2006 - 13:17

Use code like the following to create a dynamic list of options:

<?php
$terms
= taxonomy_get_children(0, 1);
 
$options = array ();
  foreach (
$terms as $term) {
     
$options [$term->tid] = $term->name;
  }
?>

$options[$term->tid] is the value that will be returned in the form

www.johnstonwebsolutions.com/we_install_modify_and_maintain_drupal_web_s...

selected value

najibx - July 18, 2008 - 02:57

So now you have list of items, how to put 'selected' on currently selected on this jump menu- instead of having the predifined selected. http://drupal.org/node/91924

 
 

Drupal is a registered trademark of Dries Buytaert.