display a taxonomy terms as autocomplete using form API I am writing my own module. I want to display a taxonomy terms as a autocomplete field in my form using form API. How can I do that?

I am using Drupal7.
Hope this will help you,

In Drupal 7, it can be done in 2 ways


By using existing taxonomy field(like TAG field), for this example I'm using field_tag

$node=node_load($nid);
$tags = array();
foreach ($node->field_tags['und'] as $item) {
  $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
}
$form['tags'] = array(
  '#type' => 'textfield',
  '#default_value' => taxonomy_implode_tags($tags),
  '#title' => 'Add Tags',
  '#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
  '#maxlength' => 1024,
  '#element_validate' => array('taxonomy_autocomplete_validate'),page
);

If you dont want to use any field then it can also be using custom autocomplete

//custom field
$form['taxonomy_term_field'] = array(
  '#type' => 'textfield',
  '#autocomplete_path' => 'get_TAXONOMY_NAME/autocomplete',
);
//menu callback for autocomplete
function HOOK_menu() {
  $items = array();
  ....
  $items['get_TAXONOMY_NAME/autocomplete/%'] = array(
    'page callback' => 'TAXONOMY_NAME_autocomplete_callback',
    'page arguments' => array(2),
    'type' => MENU_CALLBACK,
  ); 
  return $items;
}
//autocomplete callback function
function TAXONOMY_NAME_autocomplete_callback($str = 0) {
  $matches = array();
  //vocabulary id
  $vid = 2;
  $result = db_select('taxonomy_term_data', 't')
    -> fields('t', array('tid', 'name'))
    -> condition('vid', $vid, '=')
    -> condition('name', $str.'%%', 'LIKE')
    -> range(0, 10)
    -> execute();
  foreach ($result as $term) {
    $matches[$term -> tid] = check_plain($term -> name);
  }
  drupal_json_output($matches);
}

References : Existing taxonomy field and Custom autocomplete field