Get the value of options in php Drupal Form
update
I have made a Drupal Form with the Form API. I used a the element select, my question how can I get the value that the user chose? For the example: the following code. How can I know what the user chose for 'Titles plus teaser' (teaser). Thanks!
form ['planning_detail']['study_开发者_开发技巧groep'] = array (
'# type =' select ',
'# title' = 'Pick a value',
'# options' => array (
'title' => t ('Titles only '),
'teaser' => t ('Titles plus teaser'),
'full text' => t ('Full text '),
)
);
You need to make a form submit handler for your form. You can do this by creating a function called FORMNAME_submit
:
function my_form_submit(&$form, &$form_state) {
$form_state['values']['field_name'] == 'title'; // TRUE
$form['field_name'][#options][$form_state['values']['field_name']] == t('Titles only '); // TRUE
}
The form_state is an array that contains a lot of info about the state of the form, and it also has all the values that was submitted. They are by default in a flat list keyed by the field name of the form element. Submit handles is where you typical process the data the user submitted, like saving it to the database etc. Forms also has a validate that will be called before the submit handler. With the form API it's possible to create form errors if the user input doesn't validate. When an error is created with API, in the validate step, the submit function wont be called. This means you will know that when the submit function is called, that the data is error free.
Updated:
If you want to value that is displayed to the user, you can access that, by accessing the form array that is supplied in validate/submit handlers. I have shown this in the example above.
function my_form_submit(&$form, &$form_state) {
$field_key = $form_state['values']['field_name']
$value = $form['field_name']['#options'][$field_key];
drupal_set_message($value);
}
The correct attribute '#options' must in ''
Assuming you have those options placed into a select list
$form['my_select'] = array(
'#type = 'select',
'#title' = 'Pick a value',
'#options' => array(
'title' => t('Titles only'),
'teaser' => t('Titles plus teaser'),
'fulltext' => t('Full text'),
)
);
you can acces the value like this
$form['my_select']['#value']
精彩评论