I have created a content type, This content type form having a select box.

I need to develop a functionality like once the user select any option from select field the following textbox should having options like auto-complete data to respected selected option.

I can not create taxonomy and dependency because the database is very large. unable to add all data to taxonomy.

I want to do it by using ajax on change option. How can I achieve this?.

Can anyone suggest the easiest method?


I have done same with User Roles and Users. Check below code if you can find it helpful.

function custom_menu(){
    $items = array();
    $items['custom'] = array(
        'title' => 'custom',
        'description' => 'Custom',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('custom_form'),
        'access callback' => TRUE,

    // " % " is the RID coming from '#autocomplete_path'.
    $items['autocomplete/%'] = array(
        'page callback' => 'autocomplete_callback',
        'page arguments' => array(1),
        'access callback' => TRUE,
    return $items;

function custom_form($form, &$form_state){  
    $options = array(); 
    $roles = user_roles($membersonly = TRUE); // Getting list of Roles.

    // Removed "Authenticated" role from the list. 
    // That is not listed in "users_roles" Table.

    foreach ($roles as $key => $value) {
        $rid = user_role_load_by_name($value);
        // Give RID to each role as their key in array.
        $options[$rid->rid] = $value; 

    // Defining $role as selected value of Select list.
    $role = !empty($form_state['values']['roles']) ? $form_state['values']['roles'] : '';

    $form['roles'] = array(
        '#type' => 'select',
        '#title' => 'Role',
        '#options' => $options,
        '#ajax' => array(
            'wrapper' => 'auto-wrapper',
            'callback' => 'select_callback',

    $form['auto'] = array(
        '#type' => 'textfield',
        '#title' => 'Users',
        '#autocomplete_path' => 'autocomplete/' . $role, // Pass RID to path. 
        '#prefix' => '<div id="auto-wrapper">',
        '#suffix' => '</div>',

    return $form;

function select_callback($form, &$form_state){
    return $form['auto'];

function autocomplete_callback($role, $string = "") {
  $matches = array();
  if ($string) {
    $query = db_select('users', 'u');

    if(is_numeric($role) && $role != ''){
        $query->join('users_roles', 'ur', 'ur.uid = u.uid');

    $query->fields('u', array('name', 'uid'));

    if(is_numeric($role) && $role != ''){
        $query->condition('ur.rid', $role)
            ->fields('ur', array('rid'));

    $query->condition('name', db_like($string) . '%', 'LIKE')
      ->range(0, 10);

    $result = $query->execute();

    foreach ($result as $user) {
      $matches[$user->name] = check_plain($user->name);

  • Thanks!, In which file you are writing this php code ? I want to write this functionality for content type. – Devidas Kadam Jun 28 '16 at 11:22
  • You can create a custom module and add above code in .module file in your custom module directory. – Manish Sharma Jun 28 '16 at 11:27

This ought to do it:


$(function() { 
    $('#text').change(function() {
    }).change(); // Trigger the event


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
<select name="text" id="text">
    <option value="Stuff 1">Option1</option>
    <option value="Stuff 2" selected="selected">Option2</option>
    <option value="Stuff 3">Option3</option>

<input id="output" type="text" />

JSFiddle: http://jsfiddle.net/QZ3ff/999/

  • this is fine, but I want to write a module which will return data from database, How can I call the module on required page and where should I write JS code ? – Devidas Kadam Jun 28 '16 at 5:49
  • You will want to create your own custom module to hold your code, like Manish is showing below. The steps for that are outlined here: drupal.org/developing/modules/7 You can still use the .change() to trigger your custom function, but you will need to build your database query in your module. More info on static queries in Drupal: drupal.org/node/310072 The specific query depends on what information you are trying to get at in the database. If you read through those two pages while looking at Manish's code it should give you a great start. – Jeramy Jun 28 '16 at 13:10

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.