我正在尝试自学Drupal,我发现了一些我找不到的教程。
我正在尝试生成一个具有动态数量的文本字段的表单,以填充和编辑我的定制表的内容。在常规PHP中,我可以使用以下命令来实现此目的:
$count = '0';
while ($row = mysql_fetch_array ($result) {
echo "<input type='text' name='title_row".$count."' value='".$row['title']."'>"
$count = $count +1;
}有人能告诉我如何在Drupal中做到这一点(并处理提交的数据)吗?
谢谢
发布于 2010-06-17 07:38:45
检查forms API reference和Form API Quickstart Guide以了解这一点。
示例的简单版本如下所示:
/**
* Form builder function - creates definition of form
*/
function yourModule_table_edit_form($form_state) {
$form = array();
// TODO: Add code/query to populate $result, using the
// Drupal DAL functions, e.g.:
$result = db_query('SELECT * FROM {your_table}');
while ($row = db_fetch_array($result) {
// Create one textfield per row
// NOTE: assumes content of title column can be used as an array index
$form[$row['title']] = array(
'#type' => 'textfield',
'#title' => $row['title'],
'#default_value' => $row['value'], // NOTE: Just assumed the column name
'#size' => 60, // Adjust as needed
'#maxlength' => 60, // Adjust as needed
// NOTE: more options for input element definition available - check the API docs
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Form submit function
*/
function yourModule_table_edit_form_submit($form, &$form_state) {
foreach ($form_state['values'] as $row_title => $value) {
// TODO: Do something with the submitted values
}
}(注意:未测试的代码,注意打字错误和其他错误)
要准备输出的表单,您需要调用drupal_get_form('yourModule_table_edit_form')。
https://stackoverflow.com/questions/3052552
复制相似问题