0

I have created a form with fields titled State and City. After selecting the state's appropriate city records, they will be loaded into city listbox. Until now they were working fine, but after clicking the submit button I am not able to store the city's value into variable called $data1. How can I get it to return the city value?

Source code

function form_test_form($form,&$form_submit) {
  $query_state=db_select('state', 's');
  $query_state->fields('s',array('State_code','State_name'));
  $query_state->orderBy('State_name','ASC');
  $state_results = $query_state->execute();
  $options_state = array();

  foreach($state_results as $state_record) {
    $options_state[$state_record->State_code]=t($state_record->State_name);
  }

  $form['state1']= array(
    '#type' => 'select',
    '#title' => t('State'),
    '#options' =>$options_state,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => '_ajaxfunction',
      'wrapper' => 'divaroundseconddropdown'
    ),
  );


  $form['city_element_wrapper'] = array(
    '#prefix' => '<div id="divaroundseconddropdown">',
    '#suffix' => '</div>',
  );
  return $form;
}

Ajax Function

function _ajaxfunction($form, $form_state) { 
  $key = !empty($form_state['values']['state1']) ? $form_state['values']['state1'] : 1;

  $query_city=db_select('city', 'c');
  $query_city->fields('c',array('District_Code','District_Name','State_Code'));
  $query_city->condition('State_Code', $key, '=');

  $query_city->orderBy('District_Name','ASC');
  $city_results = $query_city->execute();
  $options_city = array();


  foreach ($city_results as $city_record) {
    // This is correct.
    $options_city[$city_record->District_Code]=t($city_record->District_Name);
  }

  $form['city_element_wrapper']['city']= array(
    '#type' => 'select',
    '#title' => t('District'),
    '#options' => $options_city,
    '#required' => TRUE,);

  return $form['city_element_wrapper']; 
}

Now I am trying to store the value of what I have selected in City listbox into $data1 variable but I am not able to store it, Please guide me how to store. It is printing empty value (i.e blank)

function form_test_custom_form_submit($form, $form_state) {
  $data1 = $form_state['values']['city_element_wrapper'];
  print_r($data1); // it is printing empty value
}

2 Answers 2

0

1) Try form_test_custom_form_submit($form, &$form_state) {}

2) Try to print all values. print_r($form_state['values']);

// $form_state['values']['city']; you may get values here.

You can write $form['city_element_wrapper']['city'] array inside the form_test_form itself, instead of writing inside ajax callback.

6
  • I have tried this print_r($form_state['values']); but city values not displaying. I dont know how to write ajax call back inside the form_test_form. can you send me suggestions Commented May 19, 2016 at 12:27
  • $form['submit'] = array( '#type' => 'submit', '#value' => t('Save Settings'), ); include it in form Commented May 20, 2016 at 10:47
  • @balakrishnan: drupal.stackexchange.com/questions/10112/…
    – Kalidasan
    Commented May 20, 2016 at 11:40
  • Refer this too: w3shaman.com/article/creating-ajax-dropdown-drupal-7
    – Kalidasan
    Commented May 20, 2016 at 11:40
  • Thanks @kalidasan, I tried w2shamna.com demo but not working please check demo link. can you give me ur mail id please Commented May 20, 2016 at 13:04
0
function form_test_form($form,&$form_submit) {
  $query_state=db_select('state', 's');
  $query_state->fields('s',array('State_code','State_name'));
  $query_state->orderBy('State_name','ASC');
  $state_results = $query_state->execute();
  $options_state = array();

  foreach($state_results as $state_record) {
    $options_state[$state_record->State_code]=t($state_record->State_name);
  }

  $form['state1']= array(
    '#type' => 'select',
    '#title' => t('State'),
    '#options' =>$options_state,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => '_ajaxfunction',
      'wrapper' => 'divaroundseconddropdown'
    ),
  );


  $form['city_element_wrapper'] = array(
    '#prefix' => '<div id="divaroundseconddropdown">',
    '#suffix' => '</div>',
  );
  // Submit Button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}
form_test_custom_form_submit($form, &$form_state) {
print_r($form_state['values'])
}

Try this

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.