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"> </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.