2

Screen Preview

I am using DRUPAL 7. I have created custom content types credit card detail. When adding a new card I want to select the country first and based on that loads related banks in the second drop down. I need help to resolve this issue.Thanks.

Note: country list is loading from countries module, where banks from a db table.

5

You will have to use AJAX.

Using hook_form_alter you can add ID to the element which needs to be changed.

function MYMODULE_MYFORM_ID_form_alter(&$form, &$form_state, $form_id) {

 //First add id to the field to be changed

  $form ['field_bank'][LANGUAGE_NONE][0]['value']['#prefix'] = '<div id="replace_textfield_div">';
  $form ['field_bank'][LANGUAGE_NONE][0]['value']['#suffix'] = '</div>';


//Then you can add AJAX to the countries field like this:

$form ['field_country'][LANGUAGE_NONE][0]['value']['#ajax'] = array(
  'callback' => 'MYMODULE_ajax_callback',                  
  'wrapper' => array('replace_textfield_div')
          );
 }

Now in your ajax callback function:

  function MYMODULE_ajax_callback(){
    $country = $form_state['values']['field_country'];

      if($country == 'US'){

             $options = array('Bank1'=>'Bank One', 'Bank2'=>'Bank Two');
           // and so on . You can Load Banks from DB_TABLE and place them here according to your need
             $form['field_bank']['#options'] = $options;   

       }
   //AND OTHERS according to your requirement

  return  $form['field_bank'];

   }

Please remember to rename the fields according to your forms and requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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