Drupal 表单使用 ahah 数据库中的数据自动完成,该怎么做?

发布于 2024-09-28 13:08:36 字数 4220 浏览 0 评论 0原文

我已经尝试了好几个小时了,但就是做不到。

我想要的是:

具有不同选项的单选或选择下拉菜单(来自数据库)。这部分没问题,只是一个查询和构建选项。

然后有 3 个文本字段,上面表单中的每个选项都有这 3 个文本字段的数据。

当用户单击/选择其中一个选项时,我想从数据库中自动完成 3 个文本字段。这是一个编辑功能,用户将选择一个选项,并且3个表单都是用当前数据完成的,因此用户可以更改它们,然后单击提交将更新写入数据库。

我正在使用 drupal 6 和 ahah_helper 模块。

我有这段代码,当前正在完成文本字段,但是当我更改第一个选择时,它不会更新,甚至选择值本身也不会更新,它只是返回到默认值,就像它没有被设置或存储到来自 ahah_helper 的“存储”。我是根据 ahah_helper_example 这样做的,它可能(而且可能是)完全错误,我刚刚开始使用 drupal 和表单。

function filiais_editar_form($form_state) {

//
// AHAH Helper stuff
//
$form = array();
ahah_helper_register($form, $form_state);


if (!isset($form_state['storage']['editar_filial']['filial']))
    $default_value = 1;
else 
    $default_value =  $form_state['storage']['editar_filial']['filial'];


$form['editar_filial'] = array(
    '#type'   => 'fieldset',
    '#prefix' => '<div id="editar-filial-wrapper">', 
    '#suffix' => '</div>',
    '#tree'   => TRUE,
    );

$query_result = db_query("SELECT DISTINCT ON (cidade) cidade, id_filial 
                        FROM filial_pf");
$cidades = array();
while($row = db_fetch_object($query_result))
    $cidades[$row->id_filial] = $row->id_filial . ' : ' . $row->cidade;

$form['editar_filial']['filial'] = array(
    '#type'             => 'select',
    '#title'            => "Escolha a filial que deseja editar",
    '#options'          => $cidades,
    '#default_value'    => $default_value,
    '#ahah' => array(
        'event'   => 'change',
        'path'    => ahah_helper_path(array('editar_filial')),
        'wrapper' => 'editar-filial-wrapper',
    ),
);

$form['editar_filial']['update'] = array(
    '#type'  => 'submit',
    '#value' => "Atualizar Dados",
    '#submit' => array('ahah_helper_generic_submit'),
    '#attributes' => array('class' => 'no-js'),
  );

    //$fid = $form_state['storage']['editar_filial']['filial'];
    $fid = $default_value;


    $query_result = db_query("SELECT cidade, endereco, estado 
                            FROM filial_pf
                            WHERE id_filial = '%d'",                           $fid);
    $row = db_fetch_object($query_result);
    $cidade = $row->cidade;
    $estado = $row->estado;
    $endereco = $row->endereco;

    $form['editar_filial']['cidade'] = array(
        '#type' => 'textfield',
        '#title' => "Nova Cidade da Filial",
        '#size' => 18,
        '#maxlength' => 18,
        '#required' => TRUE,
        '#default_value' => $cidade,
    );

    $estados = array(
                 "AC" => "AC",
                 "AL" => "AL",
                 "AP" => "AP",
                 "AM" => "AM",
                 "BA" => "BA",
                 "CE" => "CE",
                 "DF" => "DF",
                 "ES" => "ES",
                 "GO" => "GO",
                 "MA" => "MA",
                 "MT" => "MT",
                 "MS" => "MS",
                 "MG" => "MG",
                 "PA" => "PA",
                 "PB" => "PB",
                 "PR" => "PR",
                 "PE" => "PE",
                 "PI" => "PI",
                 "RJ" => "RJ",
                 "RN" => "RN",
                 "RS" => "RS",
                 "RO" => "RO",
                 "RR" => "RR",
                 "SC" => "SC",
                 "SP" => "SP",
                 "SE" => "SE",
                 "TO" => "TO");



    $form['editar_filial']['estado'] = array(
        '#type' => 'select',
        '#title' => "Estado",
        '#options' => $estados,
        '#required' => TRUE,
        '#default_value' => $estado,
    );

    $form['editar_filial']['endereco'] = array(
        '#type' => 'textarea',
        '#title' => "Novo Endereço da Filial",
        '#size' => 70,
        '#maxlength' => 140,
        '#required' => TRUE,
        '#default_value' => $endereco,
    );


$form['editar_filial']['salvar'] = array(
  '#type' => 'submit',
  '#value' => "Salvar",
);

return $form; } }

<代码>

I'm trying to do it for hours now, and I just can't do it.

What I want is:

A radio or select drop-down with varying options (from the database). This part is ok, it's just a query and building the options.

Then there are 3 text fields, each option from the above form has data for these 3 text fields.

When the user clicks/selects one of the options, i want to autocomplete the 3 text fields from the database. It's an edit feature, the user will select one option, and the 3 forms are completed with the current data, so the user can change them, and click submit to write the update to the database.

I'm using drupal 6 and the ahah_helper module.

I have this code, which is currently completing the textfields, but when I change the first selection, it just won't update, not even the select value itself, it just comes back to the default, like it's not being setted or stored into the 'storage' from ahah_helper. I did this based on the ahah_helper_example, it can be (and probably is) totally wrong, I just started on drupal and forms.

function filiais_editar_form($form_state) {

//
// AHAH Helper stuff
//
$form = array();
ahah_helper_register($form, $form_state);


if (!isset($form_state['storage']['editar_filial']['filial']))
    $default_value = 1;
else 
    $default_value =  $form_state['storage']['editar_filial']['filial'];


$form['editar_filial'] = array(
    '#type'   => 'fieldset',
    '#prefix' => '<div id="editar-filial-wrapper">', 
    '#suffix' => '</div>',
    '#tree'   => TRUE,
    );

$query_result = db_query("SELECT DISTINCT ON (cidade) cidade, id_filial 
                        FROM filial_pf");
$cidades = array();
while($row = db_fetch_object($query_result))
    $cidades[$row->id_filial] = $row->id_filial . ' : ' . $row->cidade;

$form['editar_filial']['filial'] = array(
    '#type'             => 'select',
    '#title'            => "Escolha a filial que deseja editar",
    '#options'          => $cidades,
    '#default_value'    => $default_value,
    '#ahah' => array(
        'event'   => 'change',
        'path'    => ahah_helper_path(array('editar_filial')),
        'wrapper' => 'editar-filial-wrapper',
    ),
);

$form['editar_filial']['update'] = array(
    '#type'  => 'submit',
    '#value' => "Atualizar Dados",
    '#submit' => array('ahah_helper_generic_submit'),
    '#attributes' => array('class' => 'no-js'),
  );

    //$fid = $form_state['storage']['editar_filial']['filial'];
    $fid = $default_value;


    $query_result = db_query("SELECT cidade, endereco, estado 
                            FROM filial_pf
                            WHERE id_filial = '%d'",                           $fid);
    $row = db_fetch_object($query_result);
    $cidade = $row->cidade;
    $estado = $row->estado;
    $endereco = $row->endereco;

    $form['editar_filial']['cidade'] = array(
        '#type' => 'textfield',
        '#title' => "Nova Cidade da Filial",
        '#size' => 18,
        '#maxlength' => 18,
        '#required' => TRUE,
        '#default_value' => $cidade,
    );

    $estados = array(
                 "AC" => "AC",
                 "AL" => "AL",
                 "AP" => "AP",
                 "AM" => "AM",
                 "BA" => "BA",
                 "CE" => "CE",
                 "DF" => "DF",
                 "ES" => "ES",
                 "GO" => "GO",
                 "MA" => "MA",
                 "MT" => "MT",
                 "MS" => "MS",
                 "MG" => "MG",
                 "PA" => "PA",
                 "PB" => "PB",
                 "PR" => "PR",
                 "PE" => "PE",
                 "PI" => "PI",
                 "RJ" => "RJ",
                 "RN" => "RN",
                 "RS" => "RS",
                 "RO" => "RO",
                 "RR" => "RR",
                 "SC" => "SC",
                 "SP" => "SP",
                 "SE" => "SE",
                 "TO" => "TO");



    $form['editar_filial']['estado'] = array(
        '#type' => 'select',
        '#title' => "Estado",
        '#options' => $estados,
        '#required' => TRUE,
        '#default_value' => $estado,
    );

    $form['editar_filial']['endereco'] = array(
        '#type' => 'textarea',
        '#title' => "Novo Endereço da Filial",
        '#size' => 70,
        '#maxlength' => 140,
        '#required' => TRUE,
        '#default_value' => $endereco,
    );


$form['editar_filial']['salvar'] = array(
  '#type' => 'submit',
  '#value' => "Salvar",
);

return $form;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2024-10-05 13:08:36

让它工作,在这里找到了一些很好的例子:

http://www.kristen.org /content/drupal-ahah-form-examples

代码如下所示:

function filiais_editar_form($form_state) {

    //
    // AHAH Helper stuff
    //
    $form = array();
    ahah_helper_register($form, $form_state);


    $form['editar'] = array(
        '#type'   => 'fieldset',
        '#prefix' => '<div id="editar-wrapper">', 
        '#suffix' => '</div>',
        '#tree'   => TRUE,
        );

    $query_result = db_query("SELECT DISTINCT ON (cidade) cidade, id_filial 
                            FROM filial_pf");
    $cidades = array();
    while($row = db_fetch_object($query_result))
        $cidades[$row->id_filial] = $row->id_filial . ' : ' . $row->cidade;


    if (!isset($form_state['values']['editar']['filial']))
        $choice = 0;
    else 
        $choice = $form_state['values']['editar']['filial'];


    $form['editar']['filial'] = array(
        '#type'             => 'radios',
        '#title'            => "Escolha a filial que deseja editar",
        '#options'          => $cidades,
        '#default_value'    => $choice,
        '#ahah' => array(
            'event'     => 'change',
            'path'      => ahah_helper_path(array('editar')),
            'wrapper'   => 'editar-wrapper',
            'method'    => 'replace',
        ),
    );

    $fid = $choice;
    if($fid != 0)
    {
        $query_result = db_query("SELECT cidade, endereco, estado 
                                FROM filial_pf
                                WHERE id_filial = '%d'", $fid);
        $row = db_fetch_object($query_result);

        $cidade = $row->cidade;
        $estado = $row->estado;
        $endereco = $row->endereco;
    }
    else
    {
        $cidade = '';
        $estado = '';
        $endereco = '';
    }   
    $form['editar']['cidade'] = array(
        '#type' => 'textfield',
        '#title' => "Nova Cidade da Filial",
        '#size' => 18,
        '#maxlength' => 18,
        '#required' => TRUE,
        '#default_value' => $cidade,
    );

    $estados = array(
        "AC" => "AC",
        "AL" => "AL",
        "AP" => "AP",
        "AM" => "AM",
        "BA" => "BA",
        "CE" => "CE",
        "DF" => "DF",
        "ES" => "ES",
        "GO" => "GO",
        "MA" => "MA",
        "MT" => "MT",
        "MS" => "MS",
        "MG" => "MG",
        "PA" => "PA",
        "PB" => "PB",
        "PR" => "PR",
        "PE" => "PE",
        "PI" => "PI",
        "RJ" => "RJ",
        "RN" => "RN",
        "RS" => "RS",
        "RO" => "RO",
        "RR" => "RR",
        "SC" => "SC",
        "SP" => "SP",
        "SE" => "SE",
        "TO" => "TO");

    $form['editar']['estado'] = array(
        '#type' => 'select',
        '#title' => "Estado",
        '#options' => $estados,
        '#required' => TRUE,
        '#default_value' => $estado,
    );

    $form['editar']['endereco'] = array(
        '#type' => 'textarea',
        '#title' => "Novo Endereço da Filial",
        '#size' => 70,
        '#maxlength' => 140,
        '#required' => TRUE,
        '#default_value' => $endereco,
    );


    $form['editar']['salvar'] = array(
      '#type' => 'submit',
      '#value' => "Salvar",
    );

    $form['#redirect'] = '';

  return $form;
}

Made it work, found some nice examples here:

http://www.kristen.org/content/drupal-ahah-form-examples

And the code looks like this:

function filiais_editar_form($form_state) {

    //
    // AHAH Helper stuff
    //
    $form = array();
    ahah_helper_register($form, $form_state);


    $form['editar'] = array(
        '#type'   => 'fieldset',
        '#prefix' => '<div id="editar-wrapper">', 
        '#suffix' => '</div>',
        '#tree'   => TRUE,
        );

    $query_result = db_query("SELECT DISTINCT ON (cidade) cidade, id_filial 
                            FROM filial_pf");
    $cidades = array();
    while($row = db_fetch_object($query_result))
        $cidades[$row->id_filial] = $row->id_filial . ' : ' . $row->cidade;


    if (!isset($form_state['values']['editar']['filial']))
        $choice = 0;
    else 
        $choice = $form_state['values']['editar']['filial'];


    $form['editar']['filial'] = array(
        '#type'             => 'radios',
        '#title'            => "Escolha a filial que deseja editar",
        '#options'          => $cidades,
        '#default_value'    => $choice,
        '#ahah' => array(
            'event'     => 'change',
            'path'      => ahah_helper_path(array('editar')),
            'wrapper'   => 'editar-wrapper',
            'method'    => 'replace',
        ),
    );

    $fid = $choice;
    if($fid != 0)
    {
        $query_result = db_query("SELECT cidade, endereco, estado 
                                FROM filial_pf
                                WHERE id_filial = '%d'", $fid);
        $row = db_fetch_object($query_result);

        $cidade = $row->cidade;
        $estado = $row->estado;
        $endereco = $row->endereco;
    }
    else
    {
        $cidade = '';
        $estado = '';
        $endereco = '';
    }   
    $form['editar']['cidade'] = array(
        '#type' => 'textfield',
        '#title' => "Nova Cidade da Filial",
        '#size' => 18,
        '#maxlength' => 18,
        '#required' => TRUE,
        '#default_value' => $cidade,
    );

    $estados = array(
        "AC" => "AC",
        "AL" => "AL",
        "AP" => "AP",
        "AM" => "AM",
        "BA" => "BA",
        "CE" => "CE",
        "DF" => "DF",
        "ES" => "ES",
        "GO" => "GO",
        "MA" => "MA",
        "MT" => "MT",
        "MS" => "MS",
        "MG" => "MG",
        "PA" => "PA",
        "PB" => "PB",
        "PR" => "PR",
        "PE" => "PE",
        "PI" => "PI",
        "RJ" => "RJ",
        "RN" => "RN",
        "RS" => "RS",
        "RO" => "RO",
        "RR" => "RR",
        "SC" => "SC",
        "SP" => "SP",
        "SE" => "SE",
        "TO" => "TO");

    $form['editar']['estado'] = array(
        '#type' => 'select',
        '#title' => "Estado",
        '#options' => $estados,
        '#required' => TRUE,
        '#default_value' => $estado,
    );

    $form['editar']['endereco'] = array(
        '#type' => 'textarea',
        '#title' => "Novo Endereço da Filial",
        '#size' => 70,
        '#maxlength' => 140,
        '#required' => TRUE,
        '#default_value' => $endereco,
    );


    $form['editar']['salvar'] = array(
      '#type' => 'submit',
      '#value' => "Salvar",
    );

    $form['#redirect'] = '';

  return $form;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文