Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have the following form:
Displayed at Page load

$form['platform'] = array(
  '#type' => 'select',
  '#title' => t('Select a Platform'),
  '#options' => array(
    0 => t('- Select -')) + $options,
  '#default_value' =>  0,
  '#required' => TRUE,
  '#ajax' => array(
    'callback' => 'ajax_select_callback',
    'wrapper' => 'replace_model_div',
    'effect' => 'slide',
   ),
);

I'm trying to pull out the $form_state['values'] to determine if another select field should be shown. However I'm not sure how this will ever get called without a submit function.
To be Displayed after option is selected

 if (isset($form_state['values']['platform'])) {
 $platform_value = $form_state['values']['platform'];
 $sqlmodel = "SELECT tid, name FROM {taxonomy_term_data} WHERE vid = $platform_value";
 $result = db_query($sqlmodel);
 $options = array();
   foreach ($result as $row) {
     $model_options[$row->vid] = t($row->name);
    }

$form['model_wrapper'] = array(
  '#type' => 'select',
  '#title' => t('Select a Model'),
  '#options' => $model_options,
  '#options' => t('$platform_value'),
  '#prefix' => '<div id="replace_model_div">',
  '#suffix' => '</div>',
);
}
return $form;
}
function ajax_select_callback($form, $form_state) {
  return $form['model_wrapper'];
}

If I am understanding AJAX properly even though on a select change my AJAX call will happen to render the model_wrapper. There is nothing in the AJAX call that will re-evaluate the if (isset function, and as such the model_wrapper section will never display.

Full story and objective: AJAX dependent select

share|improve this question
    
have you tried your code? because i can't see anything wrong with it –  Marius Ilie Jul 2 '12 at 16:47
    
@MariusIlie Indeed I have. When I make a select from the Platform select box it kicks off the AJAX call however the Model select box never appears. –  rsaturns Jul 2 '12 at 16:53
    
What do you plan to do with your form if not submit it? –  Clive Jul 2 '12 at 17:06
    
@Clive there will be a submit handler, but my hope was to have the form build itself out as the user fills it in. As the list of models is dependent on what they end up selecting in the Platform field. –  rsaturns Jul 2 '12 at 17:09
    
I haven't tested the code but if the missing submit button is causing you problems, why not just include it anyway but set it to #disabled until the rest of the form has been added? That'll achieve the same effect, and you can even hide it via css until it's ready if you really don't want to display it –  Clive Jul 2 '12 at 17:18
show 3 more comments

1 Answer 1

up vote 3 down vote accepted

The reason it's not working is because the <div> with an ID of replace_model_div doesn't exist when the form is first built. So when the AJAX replace happens, there's no target container to append it to.

All you need to do is re-structure you form slightly (and remove the second #options array added to the second select):

$form['model_wrapper_container'] = array(
  '#type' => 'container',
  '#prefix' => '<div id="replace_model_div">',
  '#suffix' => '</div>',
);

if (isset($form_state['values']['platform'])) {
  $platform_value = $form_state['values']['platform'];
  $sqlmodel = "SELECT tid, name FROM {taxonomy_term_data} WHERE vid = $platform_value";
  $result = db_query($sqlmodel);
  $model_options = array();
  foreach ($result as $row) {
    $model_options[$row->vid] = t($row->name);
  }

  $form['model_wrapper_container']['model_wrapper'] = array(
    '#type' => 'select',
    '#title' => t('Select a Model'),
    '#options' => $model_options,
  );
}

And a small change to the callback:

function ajax_select_callback($form, $form_state) {
  return $form['model_wrapper_container'];
}
share|improve this answer
    
That makes perfect sense and closes out both my issues. I get it now, basically I must form a container that will first be rendered on page load and then within that container I can have my if isset logic. Which will be overwritten via the AJAX call. Thank you very much! –  rsaturns Jul 2 '12 at 17:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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