How to insert a theme table form inside an existing form? I would like to put form fields and submits inside a table (generated with theme_table) of an existing form (callback of a hook_menu entry).

The form_id (container) :

  // Part of the existing form having a $form_id, 
  // where I want to put form elements into a table : 
  ...
  $data = mymodule_get_content_list();

  $form['mymodule_content_list'] = array(
    '#theme' =&gt; 'table',
    '#header' =&gt; $header,
    '#rows' =&gt; $data,
  );


Table builder function :

function mymodule_content_list() {

  ... Running queries to get a $content_list

  $data = array();
  foreach($content_list as $content_data) {
    $operations = array(
      'update' =&gt; array(
        '#type' =&gt; 'submit',
        '#value' =&gt; 'Update',
        '#submit' =&gt; array('mymodule_page_edit'),
      ),
    );
    $content = array(
      '#type' =&gt; 'textfield',
      '#value' =&gt; $content_data-&gt;content,
    );
    $data[] = array(
      'content' =&gt; drupal_render($content),
      'operations' =&gt; drupal_render($operations),
    );
  }

  return $data;

}


With something similar to the above, I currently get the HTML of content textfields and submit buttons in place, but they aren't used as real form elements:


When I click the submit above ('Update'), the submit function used is the first declared within the form container but not the declared array('mymodule_page_edit'). 
Using a debugger, I found out that textfields declared in the table's rows aren't passed as $form_state['values'] within the (wrong) submit function.


So the question is, how to insert form elements (fields and submits) into a drupal table, inside of an existing form (hook_menu callback) ? Is it possible ?
Here is sample example where form and tables are used together.

Step1: Gather whole data, going to use in table. 

$sample_data = array
(
  array('id' =&gt; 1, 'first_name' =&gt; 'jay', 'last_name' =&gt; 'chris'),
  array('id' =&gt; 2, 'first_name' =&gt; 'clo', 'last_name' =&gt; 'jason'),
  array('id' =&gt; 3, 'first_name' =&gt; 'len', 'last_name' =&gt; 'ken'),
);


Step2: Create table header.

$sample_header = array
(
  'first_name' =&gt; t('First Name'),
  'last_name' =&gt; t('Last Name'),
);


Step3: Create table rows and columns data.

$options = array();
foreach($sample_data as $data)
{
  $options[$data['id']] = array 
  (
    'first_name' =&gt; $data['first_name'], // 'first_name' was the key used in the header
    'last_name' =&gt; $data['last_name'], // 'last_Name' was the key used in the header
  );
}


Your data should be something like this:

Array
(
  [1] =&gt; Array
  (
    [first_name] =&gt; jay
    [last_name] =&gt; chris
  )

  [2] =&gt; Array
  (
    [first_name] =&gt; clo
    [last_name] =&gt; jason
  )

  [3] =&gt; Array
  (
    [first_name] =&gt; len
    [last_name] =&gt; ken
  )
)


Step4: Use drupal function(tableselect) for table

$form['table'] = array
(
  '#type' =&gt; 'tableselect',
  '#header' =&gt; $header,
  '#options' =&gt; $options,
);


Here is complete form for you.

function my_form($form, $form_state)
{
  $sample_data = array
  (
      array('id' =&gt; 1, 'first_name' =&gt; 'jay', 'last_name' =&gt; 'chris'),
      array('id' =&gt; 2, 'first_name' =&gt; 'clo', 'last_name' =&gt; 'jason'),
      array('id' =&gt; 3, 'first_name' =&gt; 'len', 'last_name' =&gt; 'ken'),
  );

  $sample_header = array
  (
    'first_name' =&gt; t('First Name'),
    'last_name' =&gt; t('Last Name'),
  );
  $options = array();
foreach($sample_data as $data)
{
  $options[$data['id']] = array 
  (
    'first_name' =&gt; $data['first_name'], // 'first_name' was the key used in the header
    'last_name' =&gt; $data['last_name'], // 'last_Name' was the key used in the header
  );
}

  $form['table'] = array
  (
    '#type' =&gt; 'tableselect',
    '#header' =&gt; $header,
    '#options' =&gt; $options,
  );

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


There can be some TYPO mistake, so do change in code accordingly.