1

I have made an ajax request to update the select options based on the first select field selection

but the respond only works fine with the first request despite being seen with firebug

help me solve it please

thanks in advance

here is the code

 function listings_form_alter(&$form,&$form_state,$form_id){
    $governments=array();
  if($form_id == 'place_node_form' && arg(1)=='add' && arg(2)=='place'){
    $vocabularies=taxonomy_get_vocabularies();

  foreach($vocabularies as $vocab){
      if($vocab->machine_name =='governorates'){
          $_SESSION['vid']=$vocab->vid;
          break;
      }
  }
        $options=array(0=>'choose gov');
  $g=taxonomy_get_tree($_SESSION['vid'],0,1,FALSE);
  $x=count($g);
  for($i=0;$i<$x;$i++){
                  $options[$g[$i]->tid]=$g[$i]->name;
  }



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

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

function ajax_callback($form_state,&$form){

$childs_obj=  taxonomy_get_children($_POST['governorate'],$_SESSION['vid']);
foreach($childs_obj as $child_item){
    $op[$child_item->tid]=$child_item->name;
}

$form['city']=array("#title"=>'City','#name'=>'city','#type'=>'select','#prefix' => '<div id="replace">',
        '#suffix' => '</div>','#options'=>$op);
return array(
'#type'=>'ajax', '#commands'=>array(ajax_command_html("#replace", render($form['city'])),
ajax_command_alert($_POST['governorate'])
    )

); ?>

2
  • Code is incomplete and poor formatted, its hard to read this one. provide the exact sample code. Commented Sep 27, 2012 at 10:47
  • ok i'll change . But i see its ok Commented Sep 27, 2012 at 10:48

1 Answer 1

0

Please see http://drupal.org/coding-standards, as it is hard to read.

However that said, I usually find if ajax is only working once then as the element is replaced Drupal gives it a unique id which then breaks a selector. For example #form after the ajax request becomes #form-1 and is no longer targeted. -check for this in firebug

1
  • no change happened as i see in firebug for the replaced field or the form tag Commented Sep 27, 2012 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.