0

I have created form with fields of State and City. After selecting the state appropriate city records will be loaded into city field till now working fine but after clicking the submit button I am not able to store city value into variable called $data1. Please guide me how to get it city value

function form_test_form($form,&$form_submit) 
{
$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>',
  );

}

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 into $data1 variable but I am not able to store it, Please guide me how to store

function form_test_custom_form_submit($form, $form_state){
    $data1 = $form_state['values']['city_element_wrapper']['city'];
}

1 Answer 1

0

You need to move the $form['city'] form input creation into your form_test_form function otherwise it's considered tampering with the form, so the values will get taken out.

function form_test_form($form,&$form_submit) {
  $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>',
  );

  // If there's a "State" value set, then we create the City dropdown
  if (isset($form_state['values']['state1']) && !empty($form_state['values']['state1'])) {
    $form['city_element_wrapper']['city'] = array(
      '#type' => 'select',
      '#title' => t('District'),
      '#options' => $options_city,
      '#required' => TRUE,
    );
  }

  return $form;
}

Now the callback becomes a simple return of the relevant element:

function _ajaxfunction($form, $form_state) {
  return $form['city_element_wrapper']; 
}

Finally check the values of $form_state['values'] in your submit handler:

function form_test_custom_form_submit($form, $form_state){
  var_dump($form_state['values']);
  die();
}
4
  • In ajax function I trying to get appropriate city records under state. so how should I sent that records to form? please check my ajax function Commented May 18, 2016 at 5:49
  • Well you do that same query in the form_test_form function inside the if statement right?
    – Beebee
    Commented May 18, 2016 at 9:53
  • as per my code city records are displaying but If I run the code inside the form_test_form, it is not showing anything Commented May 18, 2016 at 12:02
  • Debug. If the exact same code that works in _ajaxfunction doesn't work in form_test_form then there's something wrong with your code or the way you copy/pasted it. I can't help you with that.
    – Beebee
    Commented May 18, 2016 at 13:21

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.