How do I dynamically fill a textfield with #ajax? As the title states, I'm wondering how I dynamically fill a textfield using an AJAX callback from a "select" form field.

I'm trying to test a select form with a number of options that, when changed, populates the textfield with data. I've tried setting the textfield's "#default_value", but it doesn't seem to work. Setting "#value" does work and I do get the information to show up in the textfield but this presents another problem. When I try to go to the next step and have a button save the data, the textfield's value remains what I set it to and doesn't change. What I mean is that when I select an option in the select form, the textfield gets populated correctly but if I change the textfield and hit save, the $form_state value is still what I set it to not what I changed it to. Is what I'm trying to do even possible?

$form['profiles'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Profiles',
    '#options' =&gt; $profiles,
    '#ajax' =&gt; array(
        'callback' =&gt; 'profile_callback',
        'wrapper' =&gt; 'profile-wrapper'
    )
);

$profile = isset($form_state['values']['profiles']) ? 
             _get_profile_info($form_state['values']['profiles']) : NULL;

$form['profile_name'] = array(
    '#type' =&gt; 'textfield',
    '#prefix' =&gt; '&lt;div id="profile-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#title' =&gt; 'Profile Name',
    '#value' =&gt; $profile == NULL ? '' : $profile[0]-&gt;{'name'}
);

$form['profile_save'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Save Profile'),
    '#submit' =&gt; array(
        'save_profile'
    )
);


I'm saving the value of my textfield from the $form_state into a file for testing right now.

function save_profile($form, &amp;$form_state){
    $fh = fopen('C:\out.txt', 'w+');
    fwrite($fh, $form_state['values']['profile_name']);
}

function profile_callback($form, &amp;$form_state){
    return $form['profile_name'];
}
After messing with this for a while, I discovered that I needed to unset the textfield. Once I did this default_value started to work with ajax, below is the final code I had.

unset($form_state['input']['profile_name']);

$form['profiles'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Profiles',
    '#options' =&gt; $profiles,
    '#ajax' =&gt; array(
        'callback' =&gt; 'profile_callback',
        'wrapper' =&gt; 'profile-wrapper',
    ),
);

$profile = isset($form_state['values']['profiles']) ? _get_profile_info($form_state['values']['profiles']) : NULL;

$form['profile_name'] = array(
    '#type' =&gt; 'textfield',
    '#prefix' =&gt; '&lt;div id="profile-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#title' =&gt; 'Profile Name',
    '#default_value' =&gt; $profile == NULL ? '' : $profile[0]-&gt;{'name'},
);