Search code examples
drupal-7

Drupal7: On select of 3 dropdown list of state, city and branch it should display the address with Google map? How should i do it give me suggestions?


  1. Created the form and stored the value in Database
  2. $form["contact_options"]["state_select"] = array( "#type" => "select", "#title" => t("Select your state"), "#options" => $states_types, "#description" => t("Select state."), '#attributes' => array('id' => array('SelectType'), 'onchange' => 'getState(this.value)'), );

  3. Get the value from the Database.

  4. on using the on-change value. If i select the value of state, how i assign to another form and get result of cities according to state i chosen from database.

Solution

  •   $form['ver_ajax_dropdown']['state'] = array(
       '#title' => t('State'),
       '#type' => 'select',
       '#options' => _load_state(),
       '#ajax' => array(
       'event'=>'change',
       'callback' =>'ver_ajax_dropdown_city',
       'wrapper' => 'city-wrapper',
       ),
     );
    
      $options = array('- Select City -');
     if (isset($form_state['values']['state'])) {
     $options = _load_city($form_state['values']['state']);
    }
    
    $form['ver_ajax_dropdown']['city'] = array(
    '#title' => t('City'),
    '#type' => 'select',
    '#prefix' => '<div id="city-wrapper">',
    '#suffix' => '</div>',
     '#options' => $options,
    '#ajax' => array(
      'event'=>'change',
      'callback' =>'ver_ajax_dropdown_branch',
      'wrapper' => 'branch-wrapper',
    ),
    );
    
     $branch_options = array('- Select Branch -');
      if (isset($form_state['values']['city'])) {
       $branch_options = _load_branch($form_state['values']['city']);
     }
    
     $form['ver_ajax_dropdown']['branch'] = array(
      '#title' => t('Branch'),
      '#type' => 'select',
      '#prefix' => '<div id="branch-wrapper">',
      '#suffix' => '</div>',
      '#options' => $branch_options,
      '#ajax' => array(
      'wrapper' => 'contact_ajax_wrapper',
      'callback' => 'contact_form_ajax',
      ), 
     );
    
     function ver_ajax_dropdown_city($form, $form_state) {
      return $form['ver_ajax_dropdown']['city'];
     }
     function ver_ajax_dropdown_branch($form, $form_state) {
      return $form['ver_ajax_dropdown']['branch'];
     }
     function contact_form_ajax($form, $form_state) {
      return $form['results'];
      }
    
    
     function _load_state() {
    
      $state = array('- Select State -');
      $query = db_select("vercontact_mapping", "a");
      $query->distinct();
      $query->fields("a", array('state'));
      $query->orderBy("a.state");
      $result = $query->execute();
    
      while($row = $result->fetchObject()){
      $state[$row->state] = $row->state;
      }
      //print_r($province);
      return $state;
     }
    
     function _load_city($state) {
      $city = array('- Select City -');
      $query = db_select("vercontact_mapping", "a");
      $query->distinct();
      $query->fields("a", array('state','city'));
      $query->condition("a.state", $state);
      $query->orderBy("a.city");
      $query->groupBy("a.city");  
      $result = $query->execute();
    
     while($row = $result->fetchObject()){
      $city[$row->city] = $row->city;
     }
     //print_r($city);
      return $city;
     }
     function _load_branch($city) {
      $branch = array('- Select Branch -');
      $query = db_select("vercontact_mapping", "a");
      $query->distinct();
      $query->fields("a", array('city','branch'));
      $query->condition("a.city", $city);
      $query->orderBy("a.branch");
      $query->groupBy("a.branch");  
      $result = $query->execute();
    
      while($row = $result->fetchObject()){
       $branch[$row->branch] = $row->branch;
      }
    
    
     return $branch;
    }