5

我在制作 Drupal 模块时遇到问题。我创建了一个用于添加到数据库的表单,但我没有运气创建表单来编辑一些记录,这是我的问题。问题是当我从数据库将值加载到表单加载中并更改它们,然后在提交新值之前单击提交按钮表单刷新。所以它会更新到与原来相同的数据库中。这是一个代码:

function edit_form($form, &$form_state) {

$query = db_select('activity', 'f')
  ->fields('f')
  ->condition('IDA', $_GET['edit']);
$thefile = $query->execute();
$title = "";
$desc = "";
$file = "";
$privacy = "";
    while($record = $thefile->fetchAssoc()) 
    {
        $title = $record['title'];
        $desc = $record['description'];ick submit button form refresh before it submit new values. So it updates into database same thing as it was. Here is a good :

function edit_form($form, &$form_state) {

$query = db_select('activity', 'f') ->fields('f') ->co
        $file = $record['trainingresource'];
        $privacy = $record['privacy'];

    }
  $form['activity'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create a new activity'),
    '#tree' => TRUE,


  );
  $form['activity']['title'] = array(
      '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('Please enter the title here.'),
    '#value' => t($title),
  );
$form['activity']['description'] = array(
   '#type' => 'textarea',
    '#title' => t('Enter Description'),
  '#value' => t($desc),
    '#description' => t('Please put description here.'),

  );
 /* $form['activity']['date'] = array(
   '#type' => 'date',
    '#title' => t('Enter activity date'),

    '#description' => t('Please put activity date in here.'),
  ); */
  $form['activity']['file'] = array(
   '#type' => 'file',
    '#title' => t('Submit activity file'),
'#value' => t($file),
    '#description' => t('Please files in here.'),
  );
  $form['activity']['security'] = array(
'#type' => 'radios',
'#title' => t('Privacy'),
'#value' => t($privacy),
'#options' => array('True'=>t('True'),'False'=>t('False')),
);
  // Description

  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}

这是一个提交表单代码:

function edit_form_submit($form, $form_state) {
$idt = $_GET['edit'];
$title = trim($form_state['values']['activity']['title']);
$desc = trim($form_state['values']['activity']['description']);
//$date = trim($form_state['values']['activity']['date']['year']."-".$form_state['values']['activity']['date']['month']."-".$form_state['values']['activity']['date']['day']);
$file = "file";
$privacy = trim($form_state['values']['activity']['security']['#value']);


$nid = db_update('activity') // Table name no longer needs {}
->fields(array(
  'title' => $title,
  'description' => $desc,
  //'date' => $date,
  'trainingresource' => $file,
  'privacy' => $privacy,

))
->condition('IDA', $idt,'=')
->execute();
drupal_set_message($idt);
drupal_set_message("Added into database");
drupal_goto('activity', array('query'=>array(
'activ'=>$_GET['activ'],
)));
}

如果有人有同样的问题或知道如何解决这个问题,请帮助我。

提前致谢。

4

1 回答 1

3

首先,我想指出您的示例代码粘贴错误。我看到两个相同函数edit_form的声明。

我假设第一个声明是错误的粘贴并继续回答这个问题。

我在您的表单声明中看到的主要问题是您使用“#value”来存储默认值。请使用“#default_value”。

如果您使用#value,它会忽略用户提交的值。

  1. 阅读有关使用 #value 的更多信息
  2. 阅读有关使用 #default_value 的更多信息

例如改变,

$form['activity']['description'] = array(
  '#type' => 'textarea',
  '#title' => t('Enter Description'),
  '#value' => t($desc),
  '#description' => t('Please put description here.'),
);

$form['activity']['description'] = array(
  '#type' => 'textarea',
  '#title' => t('Enter Description'),
  '#default_value' => t($desc),
  '#description' => t('Please put description here.'),
);

此外,我强烈建议您查看此链接,该链接提供了大量与 Drupal 交互的示例。

于 2012-12-09T13:05:25.013 回答