我有一个从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;
}
以下是我对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;
}
如果我使用$ form_state ['values'] ['question']
我收到以下错误:
PDOException:SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列数与第1行的值计数不匹配:INSERT INTO {simplequiz}(问题)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']
答案 0 :(得分:9)
最佳做法是使用hook_form_validate
,仅用于验证目的,除了验证之外的任何其他内容都应在hook_form_submit
中完成。
无论哪种方式,它们都以几乎相同的方式运作。
所有表单数据都存储在$form_state['values']
中,因此要访问$form['questions']
值,只需使用$form_state['values']['questions']
。