5

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' => 'select',
    '#title' => 'Profiles',
    '#options' => $profiles,
    '#ajax' => array(
        'callback' => 'profile_callback',
        'wrapper' => 'profile-wrapper'
    )
);

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

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

$form['profile_save'] = array(
    '#type' => 'submit',
    '#value' => t('Save Profile'),
    '#submit' => 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, &$form_state){
    $fh = fopen('C:\out.txt', 'w+');
    fwrite($fh, $form_state['values']['profile_name']);
}

function profile_callback($form, &$form_state){
    return $form['profile_name'];
}
2
  • it seems like you left out the most important part of your code for us to help you debug. What does the profile_callback look like?
    – ericduran
    Nov 6, 2011 at 1:12
  • Sorry thought I included it, it just returns $form['profile_name'].
    – Sathariel
    Nov 6, 2011 at 2:19

2 Answers 2

3

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' => 'select',
    '#title' => 'Profiles',
    '#options' => $profiles,
    '#ajax' => array(
        'callback' => 'profile_callback',
        'wrapper' => 'profile-wrapper',
    ),
);

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

$form['profile_name'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div id="profile-wrapper">',
    '#suffix' => '</div>',
    '#title' => 'Profile Name',
    '#default_value' => $profile == NULL ? '' : $profile[0]->{'name'},
);
1
  • I'm trying to same thing on node-add form for specific content type, it does not seems working
    – Sharique
    Jun 13, 2013 at 10:16
2

@Sathariel I don't think you need to do unset($form_state['input']['profile_name']);

This is what I'd do to make it work, since it works for me:

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

$form['profile_name'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div id="profile-wrapper">',
    '#suffix' => '</div>',
    '#title' => 'Profile Name',
);

function profile_callback($form, &$form_state){
    $form['profile_name']['#value'] = _get_profile_info($form['profiles']['#values']);
    return $form['profile_name'];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.