AJAX callback inside hook_form_alter()

I don’t know how to implement AJAX callback in hook_form_alter().

Field is plain text by default and it is populated by NID (number) of client company. Module name is “my_car”. I didn’t used node reference on this field.

Here is the code:

function my_car_form_alter(&$  form, &$  form_state, $  form_id) {      if ($  form_id == 'car_node_form') {          $  clientCompanies = companyTitle(12); // TID 12          // Replace Client company NID with title         $  form['field_car_company']['und'][0]['value'] = array(             '#title' => 'Client company',             '#type' => 'select',             '#default_value' => $  form_state['node']->field_car_company['und'][0]['value'],             '#options' => $  clientCompanies,             '#required' => TRUE,             '#empty_option' => t('- Select -')         );     } } 

Here I use companyTitle() function to return all companies (nodes) that have TID 12 and replace field type (“textfield” with “select”) and it’s content – number with text (NID with title).

function companyTitle($  tid) {      $  results = db_query("         SELECT n.nid, n.title         FROM {node} AS n         LEFT JOIN {field_data_field_company_type} AS ct ON ct.entity_id = n.nid         WHERE n.type = 'company' AND ct.field_company_type_tid = :tid         ORDER BY n.title ASC",         array(':tid' => $  tid))->fetchAllKeyed();      return $  results; } 

I have written the function that returns NID and title of client company based on TID and title:

function ajaxCompanyTitle($  tid, $  companyName) {      $  query = db_select('node', 'company')         ->fields('company', array('nid', 'title'))         ->condition('company.type', 'company', '=')         ->condition('type.field_company_type_tid', $  tid, '=')         ->condition('company.title', '%'.db_like($  companyName).'%', 'LIKE')         ->orderBy('company.title', 'ASC')         ->range(0, 10);      $  query->leftJoin('field_data_field_company_type', 'type', 'type.entity_id = company.nid');      $  results = $  query->execute()->fetchAll();      return $  results; } 

Now is the question how to implement AJAX with the last function. I need text field (it does not to be select list) with AJAX search where NID will be rendered as title (NID remaining only in database in field_car_company_value column).

Apparently I don’t know how to implement callback function.

function my_car_form_alter(&$  form, &$  form_state, $  form_id) {      if ($  form_id == 'car_node_form') {          // Replace Client company NID with title         $  form['field_car_company']['und'][0]['value'] = array(             '#title' => 'Client company',             '#type' => 'textfield',             '#required' => TRUE,             '#ajax' => array(                 'callback' => 'ajax_callback',                 'wrapper' => 'edit-field-car-company-und-0-value',             )         );     } }  function ajax_callback($  string = '') {      $  matches = array();      if ($  string) {          $  clientCompanies = ajaxCompanyTitle(12, $  string);          foreach ($  clientCompanies as $  clientCompany) {             $  matches[$  clientCompany->title] = $  clientCompany->title;         }     }      drupal_json_output($  matches); }