0

I need to add the options array for a select field based on the first one like

<?php

function lists_form_alter(&$form,&$form_state,$form_id){
    $governments=array();
  if($form_id == 'place_node_form'){
      /*$vocabularies=  taxonomy_get_vocabularies();

      foreach($vocabularies as $vocab){
          if($vocab->machine_name =='listings'){
              //
              //$_SESSION['vid']=$vocab->vid;
              break;
          }
      }*/
            $options=array();
      $g=taxonomy_get_tree(23,0,1,FALSE);
      $x=count($g);
      for($i=0;$i<$x;$i++){
                $city[$i] = taxonomy_get_children($g[$i]->tid,23);
                      $options[$g[$i+1]->tid]=$g[$i]->name;
      }
      }
                  $options[0]='choose the government';

       $form['gov']=array('#title'=>'Gov','#name'=>'governorate','#type'=>'select','#options'=>$options,
           '#suffix'=>'<div id="replace"></div>'
           ,'#ajax'=>array('callback'=>'ajax_callback','event'=>'change','wrapper'=>'replace','method'=>'replace'));

isset($form_state['values']['governorate']) ? $op=taxonomy_get_children($form_state['values']['governorate'],23)
        : $op='';            
$form['city']=array("#title"=>'City','#name'=>'city','#type'=>'select','#prefix' => '<div id="replace">',
        '#suffix' => '</div>',
           '#options'=> $op);

} 

function ajax_callback($form_state,$form){
    $form_state['rebuild']=true;
    return $form['city'];
}

?>

what's wrong here?

2 Answers 2

1

You've got the parameters the wrong way round in your callback function; and you should probably take $form_state by reference if you need to change it:

function ajax_callback($form, &$form_state){
  $form_state['rebuild']=true;
  return $form['city'];
}
3
  • I did but still nothing changed Sep 24, 2012 at 18:37
  • please make sure that you you invoked the arguments in correct order and don't forget to look at the '&' in the form_state argument :) Sep 24, 2012 at 18:40
  • i did what do u mean INVOKED Sep 24, 2012 at 18:42
0

Thanks guys i found the solution by that answer

Is it possible to replace more than one form element (wrappers) triggered by only one #ajax trigger element?

the first answer using ajax_command_replace

and here is the final code

function lists_form_alter(&$form,&$form_state,$form_id){
 $governments=array();
  if($form_id == 'place_node_form'){

        $options=array();
  $g=taxonomy_get_tree(23,0,1,FALSE);
  $x=count($g);
  for($i=0;$i<$x;$i++){
            //$city[$i] = taxonomy_get_children($g[$i]->tid,23);
                  $options[$g[$i+1]->tid]=$g[$i]->name;
  }
  }
              $options[0]='choose the government';

      $form['gov']=array('#title'=>'Gov','#name'=>'governorate','#type'=>'select','#options'=>$options
       ,'#ajax'=>array('callback'=>'ajax_callback','event'=>'change','wrapper'=>'replace','method'=>'replace'));

$form['city']=array("#title"=>'City','#name'=>'city','#type'=>'select','#prefix' => '', '#suffix' => '','#options'=>$op);

}

function ajax_callback($form_state,$form){
$form_state['values']['governorate'] != 0 ? $op=array(0=>'zero',1=>'one')
    : $op=array(2=>'two');  

  $form['city']=array("#title"=>'City',
  '#name'=>'city','#type'=>'select',
    '#pref=> '<div id="replace">',
    '#suffix' => '</div>','#options'=>$op);

return array( '#type'=>'ajax', '#commands'=>array(ajax_command_replace ("#replace",render($form['city']))) );

}

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .