0

I am not able to get the textfield value to generate an ajax dependent dropdown option.That is when i am trying to enter a textfield value in the form and clicking a button to call the ajax function. The form value for the textfield i am getting is blank.Here's the code snippet:

/*
* function to add the images to the gallery
* @param: none
* @return: none
*/
function image_gallery_add($form, &$form_state) {
$form['#tree'] = TRUE;
$form['add_image'] = array(
    '#title' => t('Add images to gallery'),
    '#type' => 'fieldset',
);
$form['add_image']['gallery_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Please enter the gallery name'),
);

$form['add_to_dropdown'] = array(
    '#type' => 'button',
    '#value' => 'Add To Dropdown',      
    '#ajax' => array(      
        'event' => 'click',
        'callback' => 'image_gallery_select_options_callback',
        'wrapper' => 'dropdown_gallery_replace',
    ),
);  

$form_field_val = isset( $form['values']['add_image']['gallery_name'] ) ?  $form['values']['add_image']['gallery_name'] : '';
$form['add_image']['select_gallery'] = array(
    '#type' => 'select',
    '#title' => 'Select Gallery',
    // The entire enclosing div created here gets replaced when textfield gallery_name changes
    // is changed.
    '#prefix' => '<div id="dropdown_gallery_replace">',
    '#suffix' => '</div>',
    // when the form is rebuilt during ajax processing, the $selected variable
    // will now have the new value and so the models will change
    '#options' => automobile_get_model_dropdown_options($form_field_val),
    '#default_value' => '',
);

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
);
return $form;

}
/**
* Handle post-validation form submission.
*/
 function image_gallery_submit($form, &$form_state) {
    $name = $form_state['values']['add_image']['gallery_name'];
     print( $name );exit();
  }

 function automobile_get_model_dropdown_options($key = '') {
     //dpm($key);
   $options = array(
            t($key) => drupal_map_assoc(array(t($key))),    
        );
     if (isset($options[$key])) {
          return $options[$key];
      }
      else {
           return array();
      }
  }

 function image_gallery_select_options_callback($form, $form_state) {
   return $form['add_image']['select_gallery'];
 }

Can anyone suggests what i am doing wrong? I am attaching a screenshot of the situation too...enter image description here

5
  • not clear here: is the "Select Gallery" field dependent on the answer to what the user enters in "Gallery name?" Commented Jun 10, 2015 at 16:24
  • yes, the "Select Gallery" gets populated with the value, the user enters in the textbox field.. Commented Jun 10, 2015 at 17:28
  • How could Select Gallery get populated depending on a textbox field? There are infinite possibilities to be entered there? Normally the first field is a pull down or some sort of list of options... Commented Jun 10, 2015 at 20:29
  • I want to pull the value from the textfield by something like $form['values']['add_image']['gallery_name'] and make this as an option in the "Select Gallery"... Commented Jun 11, 2015 at 5:51
  • What other options go into "Select Gallery"? Where are you pulling these from? Commented Jun 11, 2015 at 19:16

1 Answer 1

0

Not sure, but from the looks of it (I think) you need to set

$form['#tree'] = TRUE;

When building your form.

1
  • I tried adding the line $form['#tree'] = TRUE in the beginning of the function but it didn't help... Commented Jun 10, 2015 at 11:30

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.