Vis*_*ani 6 drupal drupal-7 drupal-modules
我有一个从hook_form实现的表单,名为simplequiz_form()我想在下面提交之后访问它的数据是我写的代码但是我提交后似乎无法访问它的数据.我究竟做错了什么 ?
function simplequiz_form_validate($form, &$form_state) {
// here is where we will validate the data and save it in the db.
$thid = db_insert('simplequiz')
->fields(array(
'questions' => &$form_state['question'],
**I can't seem to access the value of a field questions**
))
->execute();
return $thid;
}
Run Code Online (Sandbox Code Playgroud)
下面是我对hook_form()的实现
function simplequiz_form($form, &$form_submit)
{
$form['question'] = array(
'#title' => t('Please input your question'),
'#type' => 'text_format',
'#required' => FALSE,
'#description' => t('Here is where you can enter your questions'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
Run Code Online (Sandbox Code Playgroud)
如果我使用$ form_state ['values'] ['question']
我得到以下错误:
PDOException:SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配:INSERT INTO {simplequiz}(questions)VALUES(:db_insert_placeholder_0_value,:db_insert_placeholder_0_format); simplequiz_form_submit()中的数组([:db_insert_placeholder_0_value] => [:db_insert_placeholder_0_format] => filtered_html)(/home/vishal/Dropbox/sites/dev/sites/all/modules/simplequiz/simplequiz.module的第245行).
它使用$ form_state ['values'] ['question'] ['value']
最佳实践是使用hook_form_validate
,仅用于验证目的,除了验证之外的任何其他内容都应该使用hook_form_submit
.
无论哪种方式,它们都以几乎相同的方式运作.
所有表单数据都存储在其中$form_state['values']
,因此要访问$form['questions']
值,只需使用即可$form_state['values']['questions']
.