brrocks28
11/6/2016 - 3:08 PM

Drupal autocomplete Get selected auto complete text field in form_state Drupal with hidden id http://drupalgarden.blogspot.in/2013/02/get-se

Drupal autocomplete Get selected auto complete text field in form_state Drupal with hidden id http://drupalgarden.blogspot.in/2013/02/get-selected-auto-complete-text-field.html

<?php

/**
* Implement hook_menu
**/

function food_log_menu() {
    $items = array();

    $items['logs'] = array(
        'title' => 'Logs',
        'page callback' => 'add_food_log',
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
        'access arguments' => array(),
        'access callback' => TRUE,
    );


$items['logs/food/food_info'] = array(
        'page callback' => 'find_food_info',
        'access arguments' => TRUE,
        'type' => MENU_CALLBACK,
      
    );

return $items
}


function add_food_log() {
    $output = drupal_get_form('add_food_log_form');
    return $output;
}

/**
* Implement hook_form
**/
function add_food_log_form($form, &$form_state) {
    $form['#id'] = 'add_food_log_form';
    $form['#tree'] = TRUE;

    $form['what'] = array(
        '#type' => 'textfield',
        '#title' => 'What did you eat?',
        '#weight' => 1,
        '#size'=>50,
        '#autocomplete_path' => 'logs/food/food_info',
    );

// This is hidden field to set the selected value to it from auto complete list..

    $form['fid'] = array(
        '#type' => 'hidden',
        '#value' => '',
        '#attributes' => array('id' => "hid_food_id"),
    );

$form['log_submit'] = array(
        '#type' => 'button',
        '#value' => 'Log Food',
        '#executes_submit_callback' => TRUE,
        '#submit' => array('food_log_form_submit'),
        '#weight' => 4,
    );

return $form
}

/**
* Auto complete function
**/
function find_food_info($qry) {
    global $base_url;
    $matches = array();
// Your SQL query will go here and select food_id, food_name and calories from foods table.
   After running query I have list of foods when I typed letter a or whatever you want. I have $resp array after running query.

    $results = '<table cellpadding="0" cellspacing="0" border="0">';
    foreach ($resp as $row) {
            $results_tr = '<tr width ="100%" onclick="get_food_id(' . $row["id"] . ');"> <td style = "width: 50%; font-size: 11px;" >'. $row['food_name'] . '</td><td style = "width: 50%; font-size: 11px;">'.$row['calories'].' Calories' . '</td></tr>';
      
        $matches[$row['food_name']] = $results_tr;
    }
    $results .= '</table>';

    drupal_json_output($matches);
}

/**
* Implement hook_form_submit
* You will get the auto complete selected field id in hidden field.
**/
function food_log_form_submit($form, $form_state) {
print_r($form_state);
return true;
}
?>


/**
* Implement hook_init
**/

function food_log_init() {
    drupal_add_js(drupal_get_path('module', 'food_log') . '/food_log.js');
}

When we select a perticuler food from auto complete list below we call onclick function and below javascript is called. Write below code in one file and add that file in hook_init api.

function get_food_id(id){
    
    jQuery('#hid_food_id').val(id);
    
}