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, &$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) )             $num_of_files++;     }     $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.

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Adding fields via ajax in form does not show in arrays in form_submit

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.