Skip to main content
2 of 2
added 2 characters in body
rfc1484
  • 437
  • 1
  • 4
  • 12

Getting data from a form field using JavaScript in Drupal6

I have a form with two autocomplete fields: 'province' and 'city'.

When a province it's written I would like to get the input from that field in order to use it in a query for only showing cities relative to that province in the 'city' field.

Here is the code so far.

Hook form:

function location_form(&$form_state, $unused, $form_values) {               
  $form = array();                                                              
                                                                             
  $form['state'] = array(                                                       
    '#type' => 'textfield',                                                     
    '#title' => 'Province',                                                       
    '#description' => 'Province',                                                 
    '#autocomplete_path' => 'admin/autocomplete/provinces',             
  );                                                                            
                                                                                
  $form['city'] = array (                                                       
    '#type' => 'textfield',                                                     
    '#title' => 'Cidade',                                                       
    '#description' => 'Cidade',                                                 
    '#autocomplete_path' => 'admin/autocomplete/cities'                 
  );                                                                            
                                                                                
  $form['submit'] = array(                                                      
    '#type' => 'submit',                                                        
    '#value' => 'Submit',                                                       
  );                                                                            
                                                                                
  return $form;                                                                 
}

Hook menu:

function location_menu() {
 $items['admin/autocomplete/provinces'] = array (                         
    'title' => 'Provinces autocomplete',                                                  
    'description' => 'Provinces autocomplete',                                            
    'page callback' => 'location_provinces_autocomplete',                           
    'access arguments' => array('access administration pages'),                 
    'type' => MENU_CALLBACK,                                                    
  );                                                                            
                                                                                
  $items['admin/autocomplete/cities'] = array(                          
    'title' => 'Cities autocomplete',                                           
    'description' => 'Cities autocomplete',                                     
    'page callback' => 'location_cities_autocomplete',                           
    'access arguments' => array('access administration pages'),                 
    'type' => MENU_CALLBACK,                                                    
  );        

  return $items;
}

Autocomplete functions:

function location_provinces_autocomplete($input) {                                  
  $items = array();                                                             
                                                                                
  $query = "SELECT name FROM location WHERE name LIKE '%s%%' AND level = 1";     
                                                                                
  $result = db_query($query, $input);                                           
                                                                                
  while ($row = db_fetch_object($result)) {                                     
    $items[$row->name] = $row->name;                                            
  }                                                                             
                                                                                
  print drupal_to_js($items);                                                   
                                                                                
  exit;                                                                         
}                                                                               
                                                                                
function location_cities_autocomplete($input) {                                  
  $items = array();                                                             
                                                                                
  $query = "SELECT name FROM location WHERE name LIKE '%s%%' AND level = 2";     
                                                                                
  $result = db_query($query, $input);                                           
                                                                                
                                                                                
  while ($row = db_fetch_object($result)) {                                     
    $items[$row->name] = $row->name;                                            
  }                                                                             
                                                                                
  print drupal_to_js($items);                                                   
                                                                                
  exit;                                                                         
} 

Hopefully someone can point me out to an example on how to do this or something similar.

rfc1484
  • 437
  • 1
  • 4
  • 12