I have am trying to add fields to a form in drupal via ajax. I am able to have my fields show up on the form without issue but the problem is seeing values of those fields when the form is submitted. I have read many tutorials on this tonight and I am very confused why my input fields are not even showing up in $_POST. Here is an example of what I am trying to do:

function add_product($form, &$form_state) {
    //Several other fields but the main one around this issue is below.    
    $form['img_mng'] = array(
        'label' => array(
            '#type'     =>  'markup',
            '#markup'   =>  '<h3>Upload Images</h3>',
        'file1' => array(
            '#type'     => 'file',
            '#size'     => 22,
        'addMore'   =>  array(
            '#type' =>  'button',
            '#value'    => t('Add More Images'),
            '#ajax' => array(
                'callback'  =>  'add_img_upload',
                'method'    =>  'append',
                'wrapper'   =>  'kc_img_uploads',
                'keypress'  =>  'true',

            '#prefix'               =>  '<div id="kc_img_uploads">&nbsp;</div>',

    $form['submit'] = array(
        '#type'             => 'submit',
        '#value'            => t('Add Product'),
    return $form;

function add_product_validate($form,&$form_state) {
    //Appropriate Validation
    dpm($_POST); //$_POST does not show the fields, not sure how that is even possible since the new fields are added to the form.
    dpm(get_defined_vars()); //None of the variables show the new fields.

function add_product_submit($form,&$form_state) {
    // Appropriate database actions....
    dpm($form_state); //No values for new fields.
    dpm($_POST);  //New fields do not even show in the $_POST.

function add_img_upload($form,&$form_state) {
    dpm($form_state);  //Does not show the added field below if the button is clicked more than once.
    $num_of_files = 1;
    foreach ( $form['img_mng'] as $key => $value ) {
        if ( preg_match('/^file[0-9]+$/',$key) )
    $form['img_mng'][sprintf('file%d',$num_of_files)] = array(
        '#type'     => 'file',
        '#size'     => 22,
        '#id'       => sprintf('edit_file%d',$num_of_files),
        '#name'     => sprintf('files[file%d]',$num_of_files),
        '#parents'  => sprintf('file%d',$num_of_files),
        '#array_parents'    => sprintf('file%d',$num_of_files),
    $form_state['values'][sprintf('file%d',$num_of_files)] = '';
    $form_state['file2'] = NULL;
    return $form['img_mng'][sprintf('file%d',$num_of_files)];  //The fields are added to the form every time the button is clicked.  However, the id and name of the elements are only incremented once.  ie file1 becomes file2 but never makes it to file3+.  So I will have one file1 and 1 to many file2's.

I originally tried this with managed_file type but I see several people have run into the issue of it returning an empty string. I was actually able to get it to return the field details but found clicking the upload button on any of the new fields submitted the form? Any advice on how to correct this issue with managed_file type is highly desired but will settle for file type. Thanks again.

  • 1
    I noticed an issue in add_img_upload after I posted. I had the following defined: $form_state['file2'] = NULL; it should be $form_state['input'][sprintf('file%d',$num_of_files)] = NULL; That change had no impact. – Ghost Oct 12 '12 at 4:41
  • I forgot to update this thread. I found a work around but not what I would consider an answer so I am leaving open. The problem appears to be that the $form is already re-built by the time add_img_upload is called in the example above. You can only send form fields that were already added to the $form. You make those fields not visible basically in the initial build of the form. My workaround was to always build 5 empty managed_file fields. If files already exist, I build a managed_file field for each existing file and then add the 5 empty ones. – Ghost Dec 5 '12 at 16:21

The form builder function is what Drupal refers to whenever it needs to rebuild the form. Parts of the form should not be built in the ajax callback. Instead, you can return them using something like

function add_img_upload($form,&$form_state) {
  return $form['img_mng'];

Your form builder has access to form state information, which allows you to build the form conditionally depending on the state of the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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