function YOUR_FUNCTION_THAT_DISPLAYS_FORM(&$form, &$form_state) {
  $query_state=db_select('state', 's');
  $state_results = $query_state->execute();

  $options_state = array();

  // Using fetchAll() you can iterate through a result set.
  while ($state_record = $state_results->fetchAll()) {
    // You want state code as key, not state name as key.

  // State form element.
  $form['state1']= array(
    '#type' => 'select',
    '#title' => t('State'),
    '#options' =>$options_state,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => '_ajaxfunction',
      'wrapper' => 'divaroundseconddropdown'
      'method' => 'replace',
      'effect' => 'fade',

  // City wrapper form element.
  $form['city_element_wrapper'] = array(
    '#prefix' => '<div id="divaroundseconddropdown">',
    '#suffix' => '</div>',

  return $form;

// AJAX callback 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->condition('State_Code', $key, '=');

  $city_results = $query_city->execute();
  $options_city = array();

  while ($city_record = $city_results->fetchAll()) {
    // This is correct.

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

  return $form['city_element_wrapper'];

I have tried to get city value like this 'city_element_wrapper' => $form_state['values']['city_element_wrapper']['city'], but error is coming like

Notice: Undefined index: city_element_wrapper in form_test_custom_form_submit()

How to solve it?

function form_test_custom_form_submit($form, $form_state) {
  $prog_id =1;

  db_insert('tot_prog')->fields(array('state1' => $form_state['values']['state1'],
'city_element_wrapper' => $form_state['values']['city_element_wrapper']['city'],) // i have metioned like this,  is correct???? but its shows error, how to get the select city value???
 drupal_set_message("successfully saved Settings"); 

Your error:

Notice: Undefined index: city_element_wrapper in form_test_custom_form_submit()

means that index city_element_wrapper is missing in $form_state variable. Do this:

function form_test_custom_form_submit($form, &$form_state) {
  $prog_id =1;

              'state1' => $form_state['values']['state1'],
              'city_element_wrapper' => $form_state['values']['city_element_wrapper']['city'],
            ) )

  drupal_set_message("successfully saved Settings"); 

Always take the reference of $form variables if you want to fetch values from the variables or update the values of the variable. For more details about PHP reference read this.

|improve this answer|||||

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.