2

I'm trying to add a row of form elements to my form using AHAH. My form is displayed in a table and therefore I need to add my form elements as a row.

I use the following code for my "Add More" button:

$form['addmore'] = array(
  '#type' => 'button',
  '#value' => t('Add More'),
  '#ahah' => array(
    'path' => 'mymodule/add-more',
    'wrapper' => 'mytable-tbody', // mytable-tbody is the id of my form table tbody
    'method' => 'append',
    'effect' => 'fade',
  ),   
);

Then I define a menu path for the defined path, the callback function of which does the following:

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;

$form['customer-6-first_name'] = array(
  '#type' => 'textfield',
  '#title' => '',
  '#size' => '15',
  '#maxlength' => 128,
);
... ... (more such form elements)

$cells = array();
$cells[] = drupal_render($form['customer-6-first_name']);
... ... (more such form renders within the cells array)

$output = '<tr>';
foreach ($cells as $cell) {
  $output .= _theme_table_cell($cell);
}
$output .= " </tr>\n";

drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
drupal_json($output);

On clicking on "Add More", I do get a new row, and the form elements are also displayed, however they do not get an id and a name and therefore are useless.

I'm using the method "append" so that I have to write the code for just one row. I'm sure I'm doing something wrong, but have not been able to figure that out over the last 4-6 hours.

Mukesh Agarwal
  • 204
  • 1
  • 2
  • 7
  • Try playing with the array key at - $form['customer-6-first_name'] = array( ... may be that will help.. also try with just one form element just to see if the problem is in how you are adding multiple fields at a time.. – Sumeet Pareek Feb 09 '12 at 09:41

0 Answers0