How to store second dropdown value in database

function YOUR_FUNCTION_THAT_DISPLAYS_FORM(&$form, &$form_state) {   $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();    // 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.     $options_state[$state_record->State_code]=t($state_record->State_name);   }    // 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->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();     while ($city_record = $city_results->fetchAll()) {     // 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']; } 

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???         ->execute();  drupal_set_message("successfully saved Settings");  } 
Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to store second dropdown value in database

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.