gpt4 book ai didi

forms - 在drupal模块中编辑表单?

转载 作者:行者123 更新时间:2023-12-04 20:48:10 24 4
gpt4 key购买 nike

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

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

提前致谢。

最佳答案

首先,我想指出您的示例代码粘贴错误。我看到两个相同函数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',
    '#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 交互的示例的模块。

    关于forms - 在drupal模块中编辑表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13787214/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com