我有一个hook_form(),我正在尝试将数据提交到数据库
生成表单选项后,会生成类似<option value="foo">
Foo </option>
我不是想要获得期权的“价值”
我试图抓住<option>
之间的部分,这将是'Foo'
我的表格如下:
/*
* Implentation of hook_form().
*/
function f25_favorites_form() {
$form['path_options'] = array(
'#type' => 'value',
'#value' => array('default' => t('Add a favorite'), 'foo' => t('Foo'), 'bar' => t('Bar')),
);
$form['path'] = array(
'#type' => 'select',
'#title' => t('Select Page'),
'#required' => TRUE,
'#weight' => '11',
'#options' => $form['path_options']['#value'],
);
$form[submit] = array(
'#type' => 'submit',
'#weight' => '1000000',
'#value' => t('Add')
);
return $form;
}
这是form_submit():
/*
* Write Form data to database
*/
function f25_favorites_form_submit($form, &$form_state){
global $user;
$data = array(
'uid' => $user->uid,
'path' => $form_state['values']['path'],
'title' => $title,
'weight' => $weight,
'timestamp' => time(),
);
drupal_write_record('', $data);
}
我想到有类似的东西:
'path' => $form_state['values']['path'],
但是应该是这样的:
'title' => $form_state['values']['path'][#value],
答案 0 :(得分:1)
根据Drupal docs $ form变量包含来自f25_favorites_form()的数据,所以这应该有效:
'title' => $form['path_options']['#value'][$form_state['values']['path']],
基本上你会选择所选的值(通过$ form_state ['values'] ['path']收到),因为它是$ form ['path_options'] ['#value']数组的一个键,你应该使用它找到文本。