Adding fields via ajax in form does not show in arrays in form_submit 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, &amp;$form_state) {
    //Several other fields but the main one around this issue is below.    
    $form['img_mng'] = array(
        'label' =&gt; array(
            '#type'     =&gt;  'markup',
            '#markup'   =&gt;  '&lt;h3&gt;Upload Images&lt;/h3&gt;',
        ),
        'file1' =&gt; array(
            '#type'     =&gt; 'file',
            '#size'     =&gt; 22,
        ),
        'addMore'   =&gt;  array(
            '#type' =&gt;  'button',
            '#value'    =&gt; t('Add More Images'),
            '#ajax' =&gt; array(
                'callback'  =&gt;  'add_img_upload',
                'method'    =&gt;  'append',
                'wrapper'   =&gt;  'kc_img_uploads',
                'keypress'  =&gt;  'true',

            ),
            '#prefix'               =&gt;  '&lt;div id="kc_img_uploads"&gt;&amp;nbsp;&lt;/div&gt;',
        ),
    );

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

function add_product_validate($form,&amp;$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,&amp;$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,&amp;$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 =&gt; $value ) {
        if ( preg_match('/^file[0-9]+$/',$key) )
            $num_of_files++;
    }
    $form['img_mng'][sprintf('file%d',$num_of_files)] = array(
        '#type'     =&gt; 'file',
        '#size'     =&gt; 22,
        '#id'       =&gt; sprintf('edit_file%d',$num_of_files),
        '#name'     =&gt; sprintf('files[file%d]',$num_of_files),
        '#parents'  =&gt; sprintf('file%d',$num_of_files),
        '#array_parents'    =&gt; 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.
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,&amp;$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.