Hello,
I have two taxonomy populated select boxes in my profile form created using profile2 module, say primary and secondary values. I need to populate values in the second select box according to the options selected in the the first select box. So I need to add an onchange Ajax function for the first select box. First I tried the normal way, by adding the ajax callback in form attributes but at that time I got 'illegal choice' errors. So I googled for some time time and found that we can add the ajax callback to a form filed just like other attributes in the form_alter. But it was not worked for me in my profile form. Can anybody show me the right way to achieve this? Any help will be much appreciated.
Thanks in advance.

Comments

tusharbodke’s picture

Hi,
hook_form_alter() will not work for the altering the profile form.
please try with

function HOOK_form_profile2_form_alter(&$form, &$form_state) {  
  //To access the field from profile type. 
  $form['PROFILE_TYPE_MACHINE_NAME']['FILED_NAME'];
}

Hope this will work for you.
Thanks,

Tushar Shantaram Bodake

rajeesh’s picture

Thanks for your reply. I think you didn't understand my real problem. I already have the form_alter for profile form and it is working fine. The real problem is I want to know how we can add '#ajax' attribute in the profile form alter and poulate the second select box with the selection of the first select box. I laso need to get rid of 'illegal choice' errors that triggered at the time of form submission. I hope now you can understand my problem.

tusharbodke’s picture

OK,

Can you please share your code snap ? which will easy to find cause for issue..

thanks.

Tushar Shantaram Bodake

rajeesh’s picture

Ok. I will explain my problem in detail. As explained in the first post I have two drop down boxes in user profile form, which was created using profile 2 module. I want to reload the options in the second select box according to fist select box selection. Actually we can achieve this by adding ajax attribute to the first select box and then replace the second select box options. Here is the sample code given in examples module.

function ajax_example_dependent_dropdown($form, &$form_state) {
  // Get the list of options to populate the first dropdown.
  $options_first = _ajax_example_get_first_dropdown_options();
  // If we have a value for the first dropdown from $form_state['values'] we use
  // this both as the default value for the first dropdown and also as a
  // parameter to pass to the function that retrieves the options for the
  // second dropdown.
  $selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);

  $form['dropdown_first'] = array(
    '#type' => 'select',
    '#title' => 'Instrument Type',
    '#options' => $options_first,
    '#default_value' => $selected,
    // Bind an ajax callback to the change event (which is the default for the
    // select form type) of the first dropdown. It will replace the second
    // dropdown when rebuilt
    '#ajax' => array(
      // When 'event' occurs, Drupal will perform an ajax request in the
      // background. Usually the default value is sufficient (eg. change for
      // select elements), but valid values include any jQuery event,
      // most notably 'mousedown', 'blur', and 'submit'.
      // 'event' => 'change',
      'callback' => 'ajax_example_dependent_dropdown_callback',
      'wrapper' => 'dropdown-second-replace',
    ),
  );

  $form['dropdown_second'] = array(
    '#type' => 'select',
    '#title' => $options_first[$selected] . ' ' . t('Instruments'),
    // The entire enclosing div created here gets replaced when dropdown_first
    // is changed.
   // '#prefix' => '<div id="dropdown-second-replace">',
  //  '#suffix' => '</div>',
    // when the form is rebuilt during ajax processing, the $selected variable
    // will now have the new value and so the options will change
    '#options' => _ajax_example_get_second_dropdown_options($selected),
    '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

Here 'ajax_example_dependent_dropdown' is the form id. In my case form id is 'user_profile_form', but we can't use

function user_profile_form($form, &$form_state) {

}

since it was already defined in the core user module. In my case machine name of the profile using is 'service_provider_profile'. I think we can't use profile_form_alter since it is not returning $form. So Tushar, first I need to write the similar function for the profile form( like function ajax_example_dependent_dropdown($form, &$form_state) ). I think once we get the function then rest of the things will much easier. Thanks for your patience Thushar.

kaizerking’s picture

Rajeesh ,Have you solved this? can you please share how you solved ?
I am having similar requirement, for address fields I have the table for all the countries, states.cities and want dependent drop downs for state, and city it is taking a lot of time if use with term reference field
none of the address and location modules serve my purpose

saru1683’s picture

I had same problem.
I have solve with the help of http://drupal.org/node/1117428.
Add two line of code on your hook_form_alter() after that you can access $form element on ajax callback,
If you want to alter profile register form you can use profile2 API hook_form_profile2_edit_PROFILE_TYPE_form_alter(),

function mymodule_form_alter(&$form, $form_state) {
      drupal_add_js("jQuery('.ajax-processed').once().ajaxSuccess(function() {Drupal.attachBehaviors();});", 'inline');
	profile2_attach_form($form, $form_state);


}
/**
 *  alter profile2 form
 * hook_form_profile2_edit_PROFILE_TYPE_form_alter()
 * 
 */
function mymodule_form_profile2_edit_myprofile_form_alter(&$form, $form_state) {
     //alter your form
}	

Thank you.

pavi_raghu’s picture

Hi, I created my custom module for 2 dependent dropdown, the result of which should be given to the view so that it will be filtered and view is printed. Is it possible