2010-10-21 63 views
0

我試圖做幾個小時,現在我不能這樣做。Drupal表單通過ahah自動完成數據庫中的數據,怎麼做?

我要的是:

的無線電或具有不同選項中選擇下拉(從數據庫)。這部分是好的,它只是一個查詢和構建選項。

然後有3個文本字段,上述表單中的每個選項都包含這3個文本字段的數據。

當用戶單擊/選擇其中一個選項時,我想自動填充數據庫中的3個文本字段。這是一個編輯功能,用戶將選擇一個選項,並使用當前數據完成3個表單,以便用戶可以更改它們,然後單擊「提交」將更新寫入數據庫。

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

我有這段代碼,它目前正在完成textfields,但是當我改變第一個選擇,它不會更新,甚至沒有選擇值本身,它只是回到默認值,就像它不是從ahah_helper設置或存儲到「存儲」中。我是基於ahah_helper_example完成的,它可能(也可能是)完全錯誤,我剛開始使用drupal和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; }

回答

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