在drupal模块中编辑表单?

时间:2012-12-09 12:03:49

标签: forms drupal drupal-7 edit

我在制作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'],
)));
}

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

提前致谢。

1 个答案:

答案 0 :(得分:3)

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

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

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

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

  1. Read more about use of #value
  2. Read more about use of #default_value
  3. 例如改变,

    $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.'),
    );
    

    另外,我强烈建议您查看this link这是一个提供大量与Drupal交互的示例的模块。