我在制作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,'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'],))); }
如果有人有同样的问题或知道如何解决这个问题,请帮助我.
提前致谢.
解决方法
首先,我想指出你的示例代码被错误地粘贴了.我看到两个相同函数edit_form的声明.
我假设第一个声明是错误的粘贴并继续回答这个问题.
我在表单声明中看到的主要问题是您使用“#value”来存储默认值.请使用“#default_value”.
如果使用#value,则忽略用户提交的值.
> Read more about use of #value.
> Read more about use of #default_value
例如改变,
$form['activity']['description'] = array( '#type' => 'textarea',);
至
$form['activity']['description'] = array( '#type' => 'textarea','#default_value' => t($desc),);
另外,我强烈建议您查看this link,这是一个提供大量与Drupal交互的示例的模块.