I have a dropdown with 3 records fetched from Database based on selected option from dropdown textbox is getting filled
The 3 records are:
DELL
Lenovo
WIPRO
Only DELL has its repective value in the database. Both LENOVO and WIPRO don’t have respective values.
If I select “DELL” first in the dropdown, the textbox will display DELL’s repective value. But when I select “LENOVO” it should be empty but it still shows “DELL”‘s value beacuse LENOVO doesn’t have its own value in the database, so the textbox is not getting updated on ajax call textbox, it is retaining/maintaining its previous value when the current vaule is empty.
Any help or clue would be highly appreciated.
This is my module code.
function custom_dropdown_form_system_assignment_node_form_alter(&$ form, &$ form_state, $ form_id) { $ form['field_system_make_new']= array( '#title' => t('System Make 2'), '#type' => 'select', '#options' => _system_make_module_options_list(), /* in this dropdown 3 records get filled "LENOVO" "WIPRO" "DELL" / '#ajax' => array( 'callback' =>'custom_dropdown_ajax_callback', // ajax request is called on select of "LENOVO" "WIPRO" "DELL" 'wrapper' => 'field-hdd-serial-no-add-more-wrapper', ), / wrapper for textbox which will display valued based on selected option from dropdown */ );
/* textbox is re-used from other conten type it is already present/created in some other content type */ $ hdd_serial_no_value = ""; if (!empty($ form_state['values']['field_system_make_new'])) { /* passing id of selected value from dropdown */ $ hdd_serial_no_value = _load_field_hdd_serial_no_value($ form_state['values']['field_system_make_new']); /* when "DELL" is selected in dropdown it has a repective value in DB */ /* when "LENOVO" is selected in dropdown there is no repective value in DB */ /* If i select "DELL" first in dropdown and textbox will display DELL's value But when i select "LENOVO" it still shows "DELL"'s value beacuse LENOVO doesn't have it's own value in DB so textbox is not getting updated on ajax call */ } $ form['field_hdd_serial_no']['und'][0]['value']['#default_value'] = $ hdd_serial_no_value; /* Problem is here at above textbox it is retaining/maintaining previous value when current vaule is empty */ return $ form;
}
function _system_make_module_options_list() { /* 3 rows fetched from DB "LENOVO" "WIPRO" "DELL" */ $ result = db_query("SELECT nid, title FROM
node
WHERE node.type ='computer_master'"); $ options = array('-Select System-'); foreach ($ result AS $ record) { $ options[$ record->nid] = t($ record->title); } return $ options; }
function custom_dropdown_ajax_callback($ form, $ form_state) { return $ form['field_hdd_serial_no']; }
function _load_field_hdd_serial_no_value($ entity_id) { $ field_hdd_serial_no_value = db_query("SELECT hsn.field_hdd_serial_no_value as Serial FROM
field_data_field_hdd_serial_no
as hsn WHERE hsn.entity_id = :nid and hsn.bundle = 'computer_master'", array(':nid' => $ entity_id))->fetchField(); return $ field_hdd_serial_no_value; }