I'm trying to autofill text fields after selecting an item from select list. What I mean is: first I want user to choose an item from select list and then there will be 3 more text fields and I want to give them distinct values based on which was selected.
Asked
Active
Viewed 69 times
0
-
Please, try to be more specific. Maybe add what you have tried and the problems you found. – zuazo Dec 08 '16 at 11:52
-
I have a content type in which I have a select list. For select list I used taxonomy. The select list is list of companies, and the other fields are textfields. They have information about location, country, and phone of the company. When I choose company it will have its own country, and location and phone so I want to fill them automatically. I tried with hierarchical list, but it didn't worked because there is only one list. I also tried autofill modules, but that also didn't work. @zuazo – Kris Dec 08 '16 at 12:16
1 Answers
0
You need to use Drupal "Ajax framework". Please prepare your fields in hook_form_alter function.
function hook_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
$form['select_field'] = array(
'#ajax' => array(
'callback' => '_mymodule_ajax_example_simplest_callback',
'wrapper' => 'replace_textfield_div',
),
);
// This entire form element will be replaced with an updated value.
$form['textfield_to_autofill'] = array(
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
}
}
function _mymodule_ajax_example_simplest_callback(&$form, $form_state) {
// The form has already been submitted and updated. We can return the replaced
// item as it is.
$commands = array();
if($form_state['values']['select_field'][LANGUAGE_NONE][0]['value'] == "some_value"){
$form['textfield_to_autofill'][LANGUAGE_NONE][0]['value']['#value'] = "some_value";
$commands[] = ajax_command_replace("#replace_textfield_div", render($form['textfield_to_autofill']));
}
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
}
Here helping link for ajax framework.

Kamran Zafar
- 16
- 4