AJAX form - replacing #default_value not working I have an AJAX form. I want to update the text of a textfield when a button is triggered. But this does not work. My form is below:

&lt;?php
function my_form($form, &amp;$form_state) {
  $form['ask_first_name'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Ask me my first name'),
    '#default_value' =&gt; 'I LOVE chickens',
    '#prefix' =&gt; '&lt;div id="textfields"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  $form['update'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Update'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'ajax_example_autotextfields_callback',
      'wrapper' =&gt; 'textfields',
      'effect' =&gt; 'fade',
    ),
  );

  // shouldn't this update the default value when the button is pressed??
  if (isset($form_state['values'])) {
    $form['ask_first_name']['#default_value'] = 'I HATE chickens';
  }

  return $form;
}
?&gt;


When I press the 'update' button I'm expecting the field text to change, but it doesn't. If I use #value instead of #default_value it works, but I know I shouldn't use that because I still want the user to be able to change it again.

&lt;?php
// using #value works - but should I use that?
$form['ask_first_name']['#value'] = 'I HATE chickens';
?&gt;


What is the recommended Drupal way to change the field value after AJAX is triggered?

NOTE: This answer solved the problem in my case, but as mentioned in the comments there is not access to the values in $form_state['values'] after submit. However, I'm storing the data in $form_state['storage'] so I can still access the data, though I'll probably implement ctools object cache in the end.
This is taken from the description of #default_value in the form api documentation:

The value of the form element that will be displayed or selected
initially if the form has not been submitted yet

Because your form has been submitted, it wont be using #default_value. Just use #value, the user will still be able to change it.
2021 edit.....
As people have pointed out, the above is wrong as although it will allow you to set the value, it wont allow the user to submit an altered value.
Now using Drupal 9, in a form alter, I needed to set some default field values when an ajax button (which I inserted) was clicked.
I needed to set default values on the node &quot;body&quot; field and &quot;field_url&quot; link field.
I removed the user input for these fields before setting a default value.
$userInput = $form_state-&gt;getUserInput();
unset($userInput['body']);
unset($userInput['field_url']);
$form_state-&gt;setUserInput($userInput);

$form['body']['widget'][0]['#default_value'] = 'my default value set after ajax callback';
$form['field_url']['widget'][0]['uri']['#default_value'] = 'my default value';

To be honest, I am not 100% sure it is the correct way, but it does work.