I'm working on a module that would allow a user to choose how many pictures they want to upload (1-6). AJAX will then display the appropriate amount of image upload forms. I've got the AJAX working and the first upload box works but any additional upload boxes error out with the same message.

Notice: Undefined index: image_upload(Number of box) in file_ajax_upload() (line 271 of G:\content\test\il\modules\file\file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of G:\content\test\il\modules\file\file.module).

The code seems sound to me but I can't find the problem.

$form['howmany_images'] = array(
    '#title' => t('How many images do you want to upload?'),
    '#type' => 'select',
    '#options' => array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6),
    '#ajax' => array(
      'callback' => 'add_inst_image_callback',
      'wrapper' => 'checkboxes-div',
      'method' => 'replace',
      'effect' => 'fade',
    ),

  );
  $form['checkboxes_fieldset'] = array(
    '#prefix' => '<div id="checkboxes-div">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
  );

  $_SESSION['num_checkboxes'] = !empty($form_state['values']['howmany_images']) ? $form_state['values']['howmany_images'] : 1;
  for ($i=1; $i<=$_SESSION['num_checkboxes']; $i++) {
	$form['checkboxes_fieldset']["image_upload$i"] = array(
		'#title' => t("Image $i"),
		'#type' => 'managed_file',
		'#default_value' => variable_get('image_upload', ''),
		'#upload_location' => 'public://instrument',
	);
  }

The code is modeled on the example AJAX function. The AJAX function is the same as the example.

function add_inst_image_callback($form, $form_state) {
  return $form['checkboxes_fieldset'];
}

The only other code I have that uses any of this code is in the submit for the module.

for ($i=1; $i<=$_SESSION['num_checkboxes']; $i++) {
		if ( $form_state['values']["image_upload$i"] ) {
			$file = file_load($form_state['values']['checkboxes_fieldset']["image_upload$i"]);
			$file->status = FILE_STATUS_PERMANENT;
			$file = file_save($file);			
		}
		if ($i != $_SESSION['num_checkboxes']) {
			$existing_photo = $form_state['values']['checkboxes_fieldset']["image_upload$i"] . ', ';
		} else if ($i == $_SESSION['num_checkboxes']) {
			$existing_photo = $form_state['values']['checkboxes_fieldset']["image_upload$i"];
		}
	}

The above code should look good. The way I assign multiple pictures to one object is by separating the FID's by a ', ' and that's what the code above does.
I've been working on this for a while now and I'm not sure what the problem is. Could it be something with the FID since I'm using the file_managed table?

I've tried all the fixes here, but still haven't had any luck.

Comments

blah91’s picture

I still haven't been able to figure out what's going on, I looked at the source code of the page and I'm not sure if that's the problem.
Here is what the input box looks like.
<input type="file" id="edit-image-upload1-upload" name="files[image_upload1]" size="22" class="form-file">
All boxes look like this except the number is replaced by the current number.
Thanks for any help!