Search code examples
drupalformsahah

Drupal form autocompleted with data from a database by ahah, how to do it?


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


Solution

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