Drupal表单自动完成与数据库中的数据由ahah,怎么做?

时间:2010-10-21 00:54:31

标签: drupal forms ahah

我现在正试图做几个小时,而我却做不到。

我想要的是:

具有不同选项的无线电或选择下拉列表(来自数据库)。这部分没问题,它只是一个查询并构建选项。

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

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

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

我有这个代码,目前正在填写文本字段,但是当我更改第一个选择时,它只是不会更新,甚至不会更新选择值本身,它只是回到默认状态,就像它没有被设置一样或者从ahah_helper存储到'storage'中。我是基于ahah_helper_example做到的,它可能(并且可能是)完全错误,我刚开始使用drupal和form。

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; }

// // 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", );

1 个答案:

答案 0 :(得分:0)

使它成功,在这里找到了一些很好的例子:

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;
}