4

The following code is displaying the states, but when I click the submission button, I want to display the selected state and city. I am not able to display it. How can I achieve this?

$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>',
);


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);
  }

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

  return $form['city_element_wrapper']; 
}

The following code is not working properly:

function form_test_form_submit($form, $form_state) {
  echo $prog_state = $form_state['values']['state1'];
  echo $data1 = $form_state['values']['city_element_wrapper'];
}
0

form_test_form_submit is called in php that's not displayed to user. Form is processed, it's values saved, and user is taken to desired page. Now you have 2 options:

  • In submit handler set$form_state['redirect'] to a page you like. Save values to a database. On target page read them and display.
  • In validation handler add markup elements with selected values, if you want to emphasize selection but leave user on a form.
  • 1
    i have tried but i cant get the value of echo $data1 = $form_state['values']['city_element_wrapper'];, please help me – Bala Krishnan Jun 6 '13 at 11:49
  • As I told you, nothing you output in submit will ever get displayed. Time to accept that, as that's the way things are. – Mołot Jun 6 '13 at 11:52
0

Form :

 $form['field_abt_aff_mainact'] = array(
'#type' => 'select',
'#multiple'=>TRUE,
'#size'=>5,
'#title' => t('Main Activity:'),
'#options' => array('0' => 'select','1'=>'India','2'=>'U.S','3'=>'UK'),     
'#default_value' =>$rels,
 );

Insertion to database:

$field_abt_aff_mainact=array();

foreach($form_state['values']['field_abt_aff_mainact'] as $value)
        {
        $field_abt_aff_mainact[] =$value;
        }

$insertion=db_insert('incident_occupation_disease')
          -> fields(array( 'occupation_work_main_act'=>implode(',',$field_abt_aff_mainact),
))
          ->condition('main_incident_id', $inst_id, '=')
          ->execute();

Display Default values:

To show default value of multiple select:

//Fetch values from databse 


$fetching=db_select('incident_occupation_disease','ifb')
        ->fields('ifb')
       ->condition('main_incident_id',$inst_id, '=')
       ->execute()
       ->fetchAll();



foreach($defautvalues as $default)
         {   
         $occupation_work_main_act[] = $default->occupation_work_main_act;
         }


$occupation_work_main_act=array();
$resultsed=explode(",",$occupation_work_main_act[0]);                     
$rels=array();
foreach ($resultsed as $rel)
     {
         $rels[]=$rel;
     }

Here, $rels array will be use in form API as default/selected value.

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.