How to fill form fields based on other fields values using AJAX I have 3 fields. On blur on any of them I want to look for empty ones - if blurred one has value and any of the others does not, I want to copy value.

Obtaining value is easy, simply $form_state['triggering_element']['#value'], but setting it elsewhere gets tricky.

Changing $form['item']['#default_value'] does not work if field is empty after user's edit. Changing $form['item']['#value'] makes Drupal ignore any further user input. Changing $form_state['values'] seems to do nothing. What is the correct approach that should work? When I'll know that, I'll be able to debug.
I had the same issue and this workaround did the trick. 
Basically you clear up $form_state['input'] value and rebuild the form. 

Form builder:

&lt;?php 
$form['test'] = array(
  '#type' =&gt; 'textfield', 
  '#title' =&gt; t('Test'),
  '#ajax' =&gt; array(
    'callback' =&gt; 'my_test_callback_func',
    'wrapper' =&gt; 'edit-test2',
    'progress' =&gt; array(
      'message' =&gt; '',
      'type' =&gt; 'throbber',
    ),
  ),
);

$form['test2'] = array(
  '#type' =&gt; 'textfield', 
  '#prefix' =&gt; '&lt;div id="edit-test2"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;', 
  '#title' =&gt; t('Test2'),
);
?&gt;


Ajax callback function:
This is the trickier part. 

&lt;?php 
function my_test_callback_func($form, &amp;$form_state) {
  unset($form_state['input']['test2'], $form_state['values']['test2']);
  $form['test2']['#default_value'] = 'Auto Filled!';
  $form_state['input']['test2'] = 'Auto Filled!';
  return form_builder($form['#id'], $form['test2'], $form_state);
}
?&gt;


We unset the user input (or check if it's empty), set a default value and then rebuild the form.