Drupal 6: form_state values empty on submit(Drupal 6:form_state 值在提交时为空)
问题描述
我正在尝试在 Drupal 6 中创建一个自定义表单,下面的代码似乎一切正常,包括提交时在数据库中创建了一个新条目,但所有 $form_state 值都是空的.我错过了什么?
I'm trying to create a custom form in Drupal 6 and everything seems to work okay with the code below including when submitted a new entry is created in the database however all the $form_state values are empty. What am I missing?
<?php
function rate_form($form_state) {
$form = array();
$form['rate']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 30,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['rate']['description'] = array(
'#type' => 'textarea',
'#title' => t('blah, blah'),
'#maxlength' => 1500,
);
$form['rate']['submit'] = array('#type' => 'submit', '#value' => t('Rate!'));
return $form;
}
print drupal_get_form($form_id);
function rate_form_submit($form_id, &$form_state) {
db_query("INSERT INTO {rate_comments} (name, description) VALUES ('%s', '%s')", $form_state['values']['rate']['name'], $form_state['values']['rate']['description']);
drupal_set_message(t('Thank you! Your rating has been added.'));
}
?>
推荐答案
除非你指定,$form_state['values'] 将是一个平面数组而不是一个嵌套数组,所以值将位于:
Unless you specify it, $form_state['values'] will be a flat array and not a nested one so the values will be located at:
$form_state['values']['name']
$form_state['values']['description']
您可以很容易地使用 devel 模块自行调试此问题.有了那个主动你可以做
You could have debugged this problem yourself pretty easily using the devel module. With that active you could do
function rate_form_submit($form_id, &$form_state) {
dpm($form_state);
//db_query("INSERT INTO {rate_comments} (name, description) VALUES ('%s', '%s')", $form_state['values']['rate']['name'], $form_state['values']['rate']['description']);
drupal_set_message(t('Thank you! Your rating has been added.'));
}
dpm 是 devel 定义的一个函数,它创建了一个很好的变量可视化表示,您可以在其中单击以显示/隐藏数组和类对象中的值.使用该信息,您可以确定所需值的存储位置.在您想在运行时检查变量的情况下,这是一个很好的工具.
dpm is a function that devel has defined, it creates a nice visual representation of the variable, where you click to show/hide the values inside arrays and class objects. Using that info you would have been able to fine where the values you needed was stored. It's a great tool in situations like this, where you want to inspect variables at runtime.
这篇关于Drupal 6:form_state 值在提交时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Drupal 6:form_state 值在提交时为空
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
